home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / text / tex / dvi2lj_0_49.lha / dvi2xx.c.orig < prev    next >
Text File  |  1992-09-09  |  126KB  |  3,890 lines

  1. #define VERSION "0.49"
  2. /**********************************************************************
  3.  ****************************  Intro  *********************************
  4.  **********************************************************************
  5.  * This program translates TeX's DVI-Code into device dependent
  6.  * code of either the
  7.  *
  8.  *     -   HP-LASERJET+ and compatibles (PCL), or the
  9.  *     -   IBM 3812 pageprinter
  10.  *
  11.  * depending on the preprocessor switches specified before compilation.
  12.  * The program is written to run on a PC XT/AT/PS2 under MS-DOS. It can
  13.  * be compiled nicely with MSC Rel. 3.0-5.1 with option -AL (large memory
  14.  * model).  Take care that in the CONFIG.SYS file the FILES parameter
  15.  * is set to 20; otherwise reduce MAXOPEN.  640K are recommended.
  16.  * I use link option /stack:9000 to increase runtime stack.
  17.  * It also works without modifications under Unix System V.
  18.  **********************************************************************
  19.  *            Adapted for the PC:    Gustaf Neumann
  20.  *            +1002 stuff        University of Economics
  21.  *            +3812 support      Augasse 2-6
  22.  *            +Output buffering      A-1090 Vienna, AUSTRIA
  23.  *            +lpt binary support    Tel. *43-222-340525/533
  24.  *            +pk-89 stuff                   773
  25.  *            +pixelpaths
  26.  *            +alternative directory structure
  27.  *            +code compiles also under Unix V (HP/UX)
  28.  *                      (thx Michael Haberler)
  29.  *            +huge characters (a character bigger than 32K)
  30.  *                      formats PXL1001 and PXL 1002
  31.  *                     (use: raster graphics)
  32.  *            +reduction of the produced code
  33.  *            +new options -X -Y -c -g
  34.  *            +changed options -r (LJ now default from first to last)
  35.  *                     -x,-y  (accept floats)
  36.  *            +new option -z for LJ: print testpage containing
  37.  *                     pagecounter after printjob
  38.  *            +try to overcome font limit on LJ (max 16 fonts/page) and
  39.  *             (max 32 fonts/document):
  40.  *                     additional fonts are drawn as bitmap-
  41.  *                     graphics.
  42.  *            +allows to set character close to the paperedge on LJ
  43.  *            +gf-supprt (by Joe Kelsey joe@Pacer.com)
  44.  *                gf.c and gf.h from mitdevices/dvi2ps
  45.  *            +clipping of rules
  46.  *            +OS/2 defines from Rutger Berns, apprmb@hheouh50.bitnet
  47.  *
  48.  *            BITNET/EARN:       NEUMANN at AWIWUW11
  49.  **********************************************************************
  50.  * fixes in LJ-mode:  rule-drawing,
  51.  *            characters with 127<=height<=200
  52.  *            reset printer at beginning and end of each job
  53.  *            better positioning of rules
  54.  * 30.1.89 (0.48) bug fixed for files containing >32 fonts (thanks A. Brosig),
  55.  *                different font assignment heuristic
  56.  * fixes in 3812-mode:  14.juli 87  positioning of rastered characters
  57.  *            better positioning of rules
  58.  * general fixes
  59.  * 1.1.88         page origin set to 1in/1in (hopefully everywhere)
  60.  * 22.7.88        reset y-position for each page-eject
  61.  * 15.1.89        fixing bug is space allocation for EmitFileName
  62.  *                (thanks to Bernhard Simon)
  63.  * 15.3.91 (0.49) landscape support for lj ii p, lj iii and lj 2000 
  64.  *                fixing rule drawing problems for IBM3812 in landcape mode,
  65.  *                256 character clean (lj family and IBM3812) 
  66.  **********************************************************************
  67.  * Preprocessor switches:
  68.  *      #define DEBUG    for massive printing of trace information
  69.  *               when -d cmdline option specified
  70.  *      #define IBM3812  produce output for the IBM3812 pageprinter
  71.  *      #define LJ       produce output for the HP Laserjet+ or LJ II
  72.  *      #define LJ2P     produce output for the HP Laserjet LJ IIP, LJ III
  73.  *                       or LaserJet 2000
  74.  *      #define LJ_LARGE_FONT_MEMORY  large FONT Memory for LJ printer family
  75.  *      #define DRAWGLYPH draws PK-Glyphs on stderr
  76.  *      #define USEPXL   use PXL and PK fonts rather than gf fonts
  77.  */
  78. /**********************************************************************
  79.  ************************  Global Definitions  ************************
  80.  **********************************************************************/
  81. /* #define IBM3812 */
  82. /* #define LJ */
  83. /* #define DRAWGLYPH */
  84.  
  85. #include "config.h"
  86.  
  87. #ifdef unix
  88. #define OS "Unix"
  89. #define READ_BINARY     "r"
  90. #define WRITE_BINARY    "w"
  91. #define labs(x) abs(x)
  92. #endif
  93. #ifdef amiga
  94. #define OS "Amiga"
  95. #define READ_BINARY     "r"
  96. #define WRITE_BINARY    "w"
  97. #define labs(x) abs(x)
  98. #endif
  99. #ifdef MSDOS
  100. #define OS "MS-DOS"
  101. #define READ_BINARY     "rb"
  102. #define WRITE_BINARY    "wb"
  103. #define MSC5
  104. #endif
  105. #ifdef OS2
  106. #define OS "OS/2"
  107. #define READ_BINARY     "rb"
  108. #define WRITE_BINARY    "wb"
  109. #define MSC5
  110. #endif
  111.  
  112. #include "commands.h"
  113. #include <string.h>
  114. #include <signal.h>
  115. #include <stdio.h>
  116. #include <ctype.h>
  117. #include <fcntl.h>
  118. #ifdef MSDOS
  119. #include <dos.h>     /* only for binaryopen on device  */
  120. #endif
  121. #ifdef  unix
  122. #   ifndef AIX
  123. #include <limits.h>
  124. #endif
  125. #endif
  126.  
  127. #define  DVIFORMAT    2
  128. #define  UNKNOWN     -1
  129. #define  FIRSTFNTCHAR     0
  130.  
  131. #ifdef   OPEN_MAX                    /* ... in a friendly unix system  */
  132. #define  MAXOPEN    (OPEN_MAX - 8)
  133. #else
  134. #define  MAXOPEN          12     /* limit on number of open font files */
  135. #endif
  136. #define  NFNTCHARS       LASTFNTCHAR+1
  137. #define  STACK_SIZE      100     /* DVI-stack size                     */
  138. #define  NONEXISTANT     -1      /* offset for PXL files not found     */
  139. #define  NO_FILE        ((FILE *)-1)
  140. #define  NEW(A) ((A *)  malloc(sizeof(A)))
  141. #define  EQ(a,b)        (strcmp(a,b)==0)
  142. #define  MM_TO_PXL(x)   (int)(((x)*RESOLUTION*10)/254)
  143. #define  PT_TO_PXL(x)   (int)((long)((x)*RESOLUTION*100l)/7224)
  144. #define  PT_TO_DVI(x)   (long)((x)*65536l)
  145. #define  BOPENCMD fopen
  146. #define  BINOPEN(f) BOPENCMD(f,READ_BINARY)
  147. /* SMALL_SIZE characters are loaded into font storage of the printer   */
  148. /* LARGE_SIZE characters are rastered                                  */
  149. /* HUGE_SIZE characters are not loaded into the memory of the host     */
  150. #define  SMALL_SIZE (unsigned char) 0
  151. #define  LARGE_SIZE (unsigned char) 1
  152. #define  HUGE_SIZE  (unsigned char) 2
  153. #define  HUGE_CHAR_PATTERN 32767l
  154. #define  BYTES_PER_PIXEL_LINE 500    /* max number of bytes per pixel line */
  155.  
  156.  
  157. #define PK_POST 245
  158. #define PK_PRE 247
  159. #define PK_ID 89
  160.  
  161. /* to speedup the program a little: redefinition of PixRound and PutWord */
  162. /*#define PIXROUND(x,c) ((((double)x+(double)(c>>1))/(double)c)+0.5)*/
  163. #define PIXROUND(x,c) (((x)+c)/c)
  164. #define PUTWORD(w)  EMITC((char)(w>>8)&0xff); EMITC((char)w&0xff)
  165. /*************************************************************************/
  166. #define  EMIT            fprintf              /* output a formatted string   */
  167. #define  EMITB(len,b)   fwrite(b,1,len,outfp)  /* output binary data of len  */
  168. #define  EMITWORD(w)    PUTWORD((w))         /* output a 2 byte word of data */
  169.  
  170. #define  MoveOver(b)  h += (long) b
  171. #define  MoveDown(a)  v += (long) a
  172. #define  qfprintf if (!G_quiet) fprintf
  173. #define  qprintf  if (!G_quiet) printf
  174. #define  LARGER(a,b) (((a)>(b)) ? (a) : (b))
  175.  
  176. #ifdef IBM3812
  177. #define  PRINTER      "IBM 3812 pageprinter"
  178. #define  EMITC(c)      PMPoutC(c)               /* output a single character */
  179. #define  PMPcont(l)    PMPout(-1,(long)l)         /* next l bytes continuous */
  180. #define  PMPflush      PMPout(0l,"")                     /* flush PMP-buffer */
  181. #define  EMITL(l,d)    PMPout((int)l,d)      /* EMIT-logical: via PMP-buffer */
  182. #define  RESOLUTION    240
  183. #define  hconvRESOLUTION   240
  184. #define  vconvRESOLUTION   240
  185. #define  CHAR_WIDTH_LARGE  100       /*  limit for loading into printer font */
  186. #define  CHAR_HEIGTH_LARGE 127       /*  limit for loading into printer font */
  187. #define  OUTBUFSIZE     20000        /*   size of output buffer for PMP cmds */
  188.                       /*   has to be less max(signed int)     */
  189. #define  MAXFONTSTORAGE      130000l /* font storage in the 3812 pageprinter */
  190. #define  EMITFILE_EXTENSION    ".pmp"      /* default extension of emit file */
  191. #define  XDEFAULTOFF    RESOLUTION        /* y default offset on page 1 inch */
  192. #define  YDEFAULTOFF    RESOLUTION        /* y default offset on page 1 inch */
  193. #define  CHARSTRINGMAX  80                /* bufferlength for SetString      */
  194. #define  MAX_PAGE_WIDTH  2040
  195. #define  MAX_PAGE_HEIGHT 3360
  196. /**********************************************************************/
  197. /**************  Positioning for the 3812  ****************************/
  198. /**********************************************************************/
  199. #define VERT_HALF(n) ((short)(n+1>>1)-1)
  200. #define HOR_HALF(n)  ((short)(n>>1))
  201. #define MoveHor(n)  if ((n)!=0) { PMPcont(3); PMPout(1,"\342"); EMITWORD((n)); }
  202. #define MoveVert(n) if ((n)!=0) { PMPcont(3); PMPout(1,"\343"); EMITWORD((n)); }
  203. #endif
  204.  
  205. #ifdef LJ
  206. #ifdef LJ2P
  207. #define  PRINTER       "HP LaserJet IIP"
  208. #else
  209. #define  PRINTER       "HP LaserJet"
  210. #endif LJ2P
  211. #define  RESOLUTION    300
  212. #define  hconvRESOLUTION   300
  213. #define  vconvRESOLUTION   300
  214. #ifdef LJ2P
  215. #define  CHAR_WIDTH_LARGE  16384     /* limit for loading into printer font */
  216. #define  CHAR_HEIGTH_LARGE 16384         /* y_offset reaches the same size! */
  217. #else   /* such as LJ+, LJ2 */
  218. #define  CHAR_WIDTH_LARGE  100       /* limit for loading into printer font */
  219. #define  CHAR_HEIGTH_LARGE 127           /* y_offset reaches the same size! */
  220. #endif
  221. #define  EMITFILE_EXTENSION    ".pcl"      /* default extension of emit file */
  222. #ifdef LJ_LARGE_FONT_MEMORY
  223. #define  MAX_FONTS_PER_PAGE 255         /* maximum number of fonts per page */
  224. #else
  225. #define  MAX_FONTS_PER_PAGE 16          /* maximum number of fonts per page */
  226. #endif
  227. #define  HANDLE_MAX_FONTS  255      /* max nr of fonts handled (rasterfont) */
  228. #define  FONTS_DOWNLOADABLE 32    /* max nr of fonts that can be downloaded */
  229. #define  MAXFONTSTORAGE (395l*1024l)                /* standard user memory */
  230. #define  EMITC(c)       putc(c,outfp)          /* output a single character */
  231. #define  EMITL(l,d)     EMITB(l,d)                  /* EMIT-logical = EMITB */
  232. #define  XDEFAULTOFF    RESOLUTION-54 /*x default offset on page 1 inch (LJ2)*/
  233. #define  YDEFAULTOFF    RESOLUTION+9    /* y default offset on page 1 inch */
  234. #define  max(x,y)       if ((y)>(x)) x = y
  235. #define  INT_ASCII(b,i) if (i == 0) b[0] = '\0'; else sprintf(b,"%hd",i)
  236. #endif
  237. /**********************************************************************/
  238. /***********************  external definitions  ***********************/
  239. /**********************************************************************/
  240. /*typedef  char    bool;*/
  241.  
  242. #ifndef unix
  243. #ifndef amiga
  244. long    access();
  245. FILE   *BOPENCMD();
  246. void    exit();
  247. int     fclose();
  248. int     fprintf();
  249. int     fseek();
  250. char   *index();
  251. int     printf();
  252. int     sscanf();
  253. int     strcmp();
  254. char   *strcpy();
  255. #ifdef MSC5
  256. unsigned int    strlen();
  257. #endif
  258. void    free();
  259. void    setbuf();
  260. #endif
  261. #endif
  262. char   *getenv();
  263.  
  264. #ifdef MSDOS
  265. int     intdos();
  266. #endif
  267.  
  268. #ifndef USEPXL
  269. /* interface to gf.c */
  270. extern FILE *gfin;
  271. extern int checksum;
  272. extern long tfm_wd[], char_pointer[];
  273. extern char char_exists[];
  274. extern int num_cols, num_rows, num_bytes, x_offset, y_offset;
  275. extern unsigned char bits[];
  276. extern int gf_font_max_m, gf_font_max_n, gf_font_min_n;
  277. extern int gettochar();
  278. extern void readbits();
  279. extern void readpost();
  280. extern void seekpost();
  281. extern int seekchar();
  282. #endif
  283.  
  284.  
  285. /**********************************************************************/
  286. /*************************  Global Procedures  ************************/
  287. /**********************************************************************/
  288. /* Note: Global procedures are declared here in alphabetical order, with
  289.    those which do not return values typed "void".  Their bodies occur in
  290.    alphabetical order following the main() procedure.  The names are
  291.    kept unique in the first 6 characters for portability. */
  292. double  ActualFactor();
  293. void    AllDone();
  294. #ifdef  MSDOS
  295. void    AssureBinary();  /* DOS and Microsoft C dependent !!! */
  296. #endif
  297. void    CopyFile();
  298. void    DecodeArgs();
  299. void    DoBop();
  300. long    DoConv();
  301. void    DoSpecial();
  302. void    EmitChar();
  303. void    Fatal();
  304. void    FindPostAmblePtr();
  305. void    FormFeed();
  306. void    GetBytes();
  307. void    GetFontDef();
  308. char    *GetKeyStr();
  309. bool    GetKeyVal();
  310. bool    IsSame();
  311. void    lcase();
  312. void    LoadAChar();
  313. long    NoSignExtend();
  314. /* see cautionary note in code, re arithmetic vs logical shifts */
  315. void    OpenFontFile();
  316. long    PixRound();
  317. void    PkRaster();
  318. void    PutWord();
  319. void    RasterLine();
  320. void    RasterChar();
  321. void    ReadFontDef();
  322. void    ReadPostAmble();
  323. void    SetChar();
  324. void    SetFntNum();
  325. void    SetPosn();
  326. void    SetRule();
  327. void    SetString();
  328. long    SignExtend();
  329. /* see cautionary note in code, re arithmetic vs logical shifts */
  330. void    SkipFontDef();
  331. void    Warning();
  332. unsigned char   skip_specials() ;
  333.  
  334. #ifdef IBM3812
  335. void    PMPout();
  336. void    PMPoutC();
  337. #endif
  338.  
  339. /**********************************************************************/
  340. /***********************  Font Data Structures  ***********************/
  341. /**********************************************************************/
  342.  
  343. struct char_entry {             /* character entry */
  344. #ifdef USEPXL
  345.     unsigned short  width, height;      /* width and height in pixels */
  346.     short   xOffset, yOffset, yyOffset; /* x offset and y offset in pixels*/
  347. #endif
  348.     struct {
  349.         bool isloaded;
  350.         union {
  351.             long    fileOffset;
  352.             long    *pixptr;
  353.         } address;
  354.     } where;
  355.     long    tfmw;             /* TFM width                 */
  356.     long    cw;               /* character width in pixels */
  357.     unsigned char   flag_byte;          /* for PK-files    */
  358.     unsigned char   charsize;
  359. };
  360. struct font_entry {    /* font entry */
  361.     long    k, c, s, d;
  362.     int     a, l;
  363.     char n[STRSIZE];          /* FNT_DEF command parameters           */
  364.     long    font_mag;         /* computed from FNT_DEF s and d parameters */
  365.     /*char psname[STRSIZE];*/ /* PostScript name of the font          */
  366.     char    name[STRSIZE];    /* full name of PXL file                */
  367.     FILE * font_file_id;      /* file identifier (NO_FILE if none)    */
  368. #ifdef USEPXL
  369.     long    magnification;    /* magnification read from PXL file     */
  370.     long    designsize;       /* design size read from PXL file       */
  371. #endif
  372.     struct char_entry ch[NFNTCHARS];   /* character information       */
  373.     struct font_entry *next;
  374.     unsigned short ncdl;      /* #of different chars actually downloaded */
  375.     unsigned short plusid;    /* Font id in Printer                    */
  376.     bool used_on_this_page;
  377.     enum PxlId {
  378.         id1001, id1002, pk89    } id;
  379. #ifdef LJ
  380.     unsigned short max_width, max_height, max_yoff;
  381. #endif
  382. };
  383.  
  384.  
  385. struct pixel_list {
  386.     FILE *pixel_file_id;    /* file identifier  */
  387.     int     use_count;      /* count of "opens" */
  388. };
  389. /**********************************************************************/
  390. /*************************  Global Variables  *************************/
  391. /**********************************************************************/
  392. long    FirstPage  = -1000000;  /* first page to print (uses count0)   */
  393. long    LastPage   = 1000000;   /* last page to print                  */
  394. long    PrintPages = 1000000;   /* nr of pages to print                */
  395. char    G_progname[STRSIZE];    /* program name                        */
  396. char    filename[STRSIZE];      /* DVI file name                       */
  397. char    rootname[STRSIZE];      /* DVI filename without extension      */
  398. char    *PXLpath = FONTAREA;    /* PXL path name for search            */
  399. char    *HeaderFileName = "";   /* file name & path of Headerfile      */
  400. char    *EmitFileName = "";     /* file name & path for output         */
  401. #ifdef IBM3812
  402. bool    FirstAlternate = FALSE; /* first page from alternate casette ? */
  403. #endif
  404. bool    Reverse = FALSE;        /* process DVI pages in reverse order? */
  405. bool    Landscape = FALSE;      /* print document in ladscape mode     */
  406. bool    ResetPrinter = TRUE;    /* reset printer at the begin of the job*/
  407. bool    DoublePage = FALSE;     /* printing on both sides of a page    */
  408. short   PageParity = 1;
  409. #ifdef LJ
  410. bool    PrintTestPage = FALSE;  /* print testpage with pagecounter after job */
  411. unsigned short pagesize = 0;    /* page size value                      */
  412. unsigned short pgsiz_dots = 0;  /* page size in dots (for rule-clipping)*/
  413. #endif
  414. short   G_errenc = 0;           /* has an error been encountered?      */
  415. bool    G_header = FALSE;       /* copy header file to output?         */
  416. bool    G_quiet = FALSE;        /* for quiet operation                 */
  417. bool    G_noverbatim = TRUE;    /* inform user about pxl-files used    */
  418. bool    G_nowarn = FALSE;       /* don't print out warnings            */
  419. short   x_origin = XDEFAULTOFF; /* x-origin in dots                    */
  420. short   y_origin = YDEFAULTOFF; /* y-origin in dots                    */
  421. short   x_goffset;              /* global x-offset in dots             */
  422. short   y_goffset;              /* global y-offset in dots             */
  423. unsigned short ncopies = 1;     /* number of copies to print           */
  424. long    hconv, vconv;           /* converts DVI units to pixels        */
  425. long    den;                    /* denominator specified in preamble   */
  426. long    num;                    /* numerator specified in preamble     */
  427. long    h;                      /* current horizontal position         */
  428. long    hh = 0;                 /* current h on device                 */
  429. long    v;                      /* current vertical position           */
  430. long    vv = 0;                 /* current v on device                 */
  431. long    mag;                    /* magnification specified in preamble */
  432. long    usermag = 0;            /* user specified magnification        */
  433. int     ndone = 0;              /* number of pages converted           */
  434. int     nopen = 0;              /* number of open PXL files            */
  435. FILE  *outfp = NULL;            /* output file                         */
  436. FILE  *pxlfp;                   /* PXL file pointer                    */
  437. FILE  *dvifp  = NULL;           /* DVI file pointer                    */
  438. struct font_entry *prevfont = NULL; /* font_entry pointer previous font*/
  439. struct font_entry *fontptr;     /* font_entry pointer                  */
  440. struct font_entry *hfontptr = NULL; /* font_entry pointer              */
  441. struct font_entry *pfontptr = NULL; /* previous font_entry pointer     */
  442. struct pixel_list pixel_files[MAXOPEN+1]; /* list of open PXL files    */
  443. long    postambleptr;           /* Pointer to the postamble            */
  444. long    ppagep;                 /* previous page pointer               */
  445. static int      last_ry = UNKNOWN;      /* last y-position on page     */
  446. static int      last_rx = UNKNOWN;      /* last x-position on page     */
  447. long   StartPrintPages;         /* notpad for double paged output      */
  448. int    WouldPrint    = 0;
  449. bool   ZeroPage = FALSE;        /* Document starts with a Zero Page    */
  450. bool   EvenPage = FALSE;        /* Document starts with an even Page   */
  451. long   LastPtobePrinted = 0;
  452. int    G_ncdl = 0;
  453.  
  454. long    allocated_storage = 0; /* size of mallocated storage (statistics) */
  455. long    power[32] ;
  456. long    gpower[33] ;
  457.  
  458.  
  459. #ifdef DEBUG
  460. bool Debug = FALSE;
  461. #endif
  462.  
  463. #ifdef LJ
  464. int     fonts_used_on_this_page = MAX_FONTS_PER_PAGE+1;
  465. char    rasterfont[HANDLE_MAX_FONTS];
  466.     /* raster if fonts/page>MAX_FONTS_PER_PAGE*/
  467. #endif
  468.  
  469. long    used_fontstorage = 0;
  470.  
  471. #ifdef IBM3812
  472. char    PMPformat[20];
  473. char    CharString[CHARSTRINGMAX];
  474. unsigned int CharStringPos = 0;
  475. #define CharStringOut \
  476.     if (CharStringPos>0) { \
  477.         PMPcont(CharStringPos+1);\
  478.         PMPoutC((unsigned char)CharStringPos);\
  479.         PMPout(CharStringPos, CharString); \
  480.         CharStringPos=0; }
  481. #endif
  482.  
  483. #ifdef TIMING
  484. /************************timing stuff*********************/
  485. #include <sys/timeb.h>
  486. void ftime();
  487. struct timeb timebuffer;
  488. double  start_time;
  489. #endif
  490.  
  491.  
  492. /**********************************************************************/
  493. /*******************************  main  *******************************/
  494. /**********************************************************************/
  495. void
  496. main(argc, argv)
  497. int     argc;
  498. char    *argv[];
  499. {
  500.     struct stack_entry {  /* stack entry */
  501.         long    h, v, w, x, y, z;  /* what's on stack */
  502.     };
  503.     short   command;          /* current command                         */
  504.     long    count[10];        /* the 10 counters at begining of each page*/
  505.     long    cpagep;           /* current page pointer                    */
  506.     bool Emitting = FALSE;    /* outputting typsetting instructions?     */
  507.     int     i;                /* command parameter; loop index           */
  508.     int     k;                /* temporary parameter                     */
  509.     char    n[STRSIZE];       /* command parameter                       */
  510.     int     PassNo = 0;       /* which pass over the DVI page are we on? */
  511.     bool SkipMode = FALSE;    /* in skip mode flag                       */
  512.     int     sp;               /* stack pointer                           */
  513.     struct stack_entry stack[STACK_SIZE];  /* stack                      */
  514.     char    SpecialStr[STRSIZE]; /* "\special" strings                   */
  515.     long    val, val2;        /* temporarys to hold command information  */
  516.     long    w;                /* current horizontal spacing              */
  517.     long    x;                /* current horizontal spacing              */
  518.     long    y;                /* current vertical spacing                */
  519.     long    z;                /* current vertical spacing                */
  520.  
  521.  
  522.     extern  char *sys_errlist[];
  523.     extern  int     errno;
  524.  
  525.     setbuf(stderr, NULL);
  526.     (void) strcpy(G_progname, argv[0]);
  527.     DecodeArgs( argc, argv );
  528.  
  529.     power [ 0 ] = 1 ;
  530.     for ( i = 1 ; i <= 31 ; i ++)
  531.         power [ i ] = power [ i - 1 ] << 1 ;
  532.     gpower[0] = 0l ;
  533.     for ( i = 1 ; i <= 32 ; i ++)
  534.         gpower[i] = gpower[i - 1] + power[i - 1] ;
  535.  
  536.     if ((i = (int) NoSignExtend(dvifp, 1)) != PRE)  {
  537.         Fatal(
  538.         "%s: PRE doesn't occur first--are you sure this is a DVI file?\n\n",
  539.                 G_progname);
  540.     }
  541.     i = (int) SignExtend(dvifp, 1);
  542.     if (i != DVIFORMAT)  {
  543.         Fatal( "%s: DVI format = %d, can only process DVI format %d files\n\n",
  544.                 G_progname, i, DVIFORMAT);
  545.     }
  546.  
  547.     if (*EmitFileName == '-')
  548.         outfp = stdout;
  549.     else
  550.         if ((outfp = fopen(EmitFileName, WRITE_BINARY)) == NULL)
  551.         Fatal("opening output file: fopen(%s) : %s",
  552.             EmitFileName,sys_errlist[errno]);
  553.  
  554. #ifdef MSDOS
  555.     AssureBinary(outfp);
  556. #endif
  557.  
  558. #ifdef TIMING
  559.     ftime(&timebuffer);
  560.     start_time = (timebuffer.time) + (timebuffer.millitm) / 1000.0;
  561. #endif
  562.  
  563.     /* it is important that these be the very first things output !!! */
  564.     if ( G_header )
  565.         CopyFile( HeaderFileName );
  566.  
  567.     /*****************************/
  568.     /*for( i0=0; i0<nif; i0++ )  */    /* copy all included files */
  569.     /*    CopyFile( Ifile[i0] ); */
  570.     /*****************************/
  571.  
  572. #ifdef IBM3812
  573.     PMPout(3, "\307\310\366");          /* unload all fonts and macros */
  574.     EMITWORD(MAX_PAGE_WIDTH);
  575.     EMITWORD(MAX_PAGE_HEIGHT);
  576.     if (Landscape)
  577.         PMPout(2, "\322\1");
  578. #endif
  579. #ifdef LJ
  580.     if (ResetPrinter)
  581.         EMIT(outfp, "\033E");
  582.     if (Landscape)
  583.         EMIT(outfp, "\033&l1O\033*rF");
  584.     if (pagesize>0)
  585.         EMIT(outfp, "\033&l%hdaE\033&aL",pagesize);
  586.     else
  587.         EMIT(outfp, "\033&lE\033&aL");
  588.  
  589.     if (ncopies>1)
  590.         EMIT(outfp, "\033&l%hdX",ncopies);
  591. #endif
  592.     if (DoublePage) {
  593.          StartPrintPages = PrintPages;
  594. #ifdef IBM3812
  595.          Reverse = (bool)!Reverse; /* perverse and strange */
  596. #endif
  597.  
  598.     }
  599.     if (Reverse) {
  600. #ifdef DEBUG
  601.         if (Debug)
  602.               fprintf(stderr, "reverse\n");
  603. #endif
  604.         ReadPostAmble(TRUE);
  605.         fseek(dvifp, ppagep, 0);
  606.     } else {
  607.         ReadPostAmble(TRUE);
  608.         fseek(dvifp,  14l, 0);
  609.         k = (int) NoSignExtend(dvifp, 1);
  610.         GetBytes(dvifp, n, k);
  611.     }
  612.     PassNo = 0;
  613.  
  614.     while (TRUE)  {
  615.         command = (short) NoSignExtend(dvifp, 1)   ;
  616. #ifdef DEBUG
  617.         if (Debug)
  618.                fprintf(stderr,"CMD:\t%d\n", command);
  619. #endif
  620.         switch (command)  {
  621.         case SET1:
  622.         case SET2:
  623.         case SET3:
  624.         case SET4:
  625.             val = NoSignExtend(dvifp, (int) command - SET1 + 1);
  626.             if (!SkipMode)
  627.                 SetChar(val, command, PassNo, TRUE,FALSE);
  628.             break;
  629.         case SET_RULE:
  630.             val = NoSignExtend(dvifp, 4);
  631.             val2 = NoSignExtend(dvifp, 4);
  632.             if (Emitting)
  633.                 SetRule(val, val2, 1);
  634.             break;
  635.         case PUT1:
  636.         case PUT2:
  637.         case PUT3:
  638.         case PUT4:
  639.             val = NoSignExtend(dvifp, (int) command - PUT1 + 1);
  640.             if (!SkipMode)
  641.                 SetChar(val, command, PassNo, TRUE,FALSE);
  642.             break;
  643.         case PUT_RULE:
  644.             val = NoSignExtend(dvifp, 4);
  645.             val2 = NoSignExtend(dvifp, 4);
  646.             if (Emitting)
  647.                 SetRule(val, val2, 0);
  648.             break;
  649.         case NOP:
  650.             break;
  651.         case BOP:
  652.             cpagep = ftell(dvifp) - 1;
  653.             for (i = 0; i <= 9; i++)
  654.                 count[i] = NoSignExtend(dvifp, 4);
  655.             ppagep = NoSignExtend(dvifp, 4);
  656.             h = v = w = x = y = z = 0;
  657.             hh = vv = 0;
  658.             sp = 0;
  659.             fontptr = NULL;
  660.             prevfont = NULL;
  661.             DoBop();
  662.             if ( count[0] < FirstPage || count[0] > LastPage )
  663.                 SkipMode = TRUE;
  664.             else
  665.                 SkipMode = FALSE;
  666.             if (DoublePage && !SkipMode) {
  667.                if (PassNo == 0) {
  668.                   LastPtobePrinted=count[0];
  669.                   if (!Reverse && (WouldPrint == 0)) {
  670.                       if (count[0] == 0l) {
  671.                          ZeroPage = TRUE;
  672.                          EvenPage = FALSE;
  673.                       }
  674.                       else {
  675.                          EvenPage = (bool) ((int)count[0]%2 == 0);
  676.  
  677.                          if (EvenPage && PageParity==1) {
  678.                            WouldPrint ++;
  679.                            qfprintf(stderr,"[EvenPage] ");
  680.                            FormFeed();
  681.                          }
  682.                       }
  683.                   }
  684.                   WouldPrint ++;
  685. /*
  686.     printf("doublepage %d, Reverse %d, WouldPrint %d, fpZ %d\n",
  687.         (int)DoublePage, (int)Reverse, (int)WouldPrint, (int)ZeroPage);
  688. */
  689.                }
  690.                SkipMode =
  691.                    (bool)(PageParity != (short)(count[0]%2));
  692.  
  693.                if (count[0]==0l) SkipMode=(bool)!SkipMode;
  694.             }
  695.             Emitting = (bool)((PassNo != 0) && !SkipMode);
  696.             if ( !SkipMode ) {
  697.                 if ( PassNo == 0)
  698.                     qfprintf(stderr, "[%ld", count[0]);
  699.             }
  700.             break;
  701.         case EOP:
  702.             if ( !SkipMode ) {
  703.                 if ( PassNo == 0 ) {
  704.                     /* start second pass on current page */
  705.                     fseek(dvifp, cpagep, 0);
  706.                     PassNo = 1;
  707.                 } else {
  708.                    /* end of second pass, and of page processing */
  709.  
  710.                     last_ry = UNKNOWN;
  711.                     FormFeed();
  712.                     ++ndone;
  713.  
  714.                     qfprintf(stderr, "] ");
  715.                     if ( (ndone % 10) == 0 )
  716.                           qfprintf(stderr, "\n");
  717.  
  718.                     if (DoublePage) --PrintPages;
  719.                     if (--PrintPages < 1) AllDone(TRUE);
  720.                     PassNo = 0;
  721.                 }
  722.             } else
  723.                 PassNo = 0;
  724.  
  725.             if ( PassNo == 0 && Reverse ) {
  726.                 if ( ppagep > 0 )
  727.                     fseek(dvifp, ppagep, 0);
  728.                 else {
  729.                    if (DoublePage && !SkipMode)
  730.                      ZeroPage = (bool)(count[0]==0l);
  731.  
  732.                    if (ZeroPage)
  733.                      EvenPage = FALSE;
  734.                    else
  735.                      EvenPage = (bool) ((int)LastPtobePrinted%2 == 0);
  736.  
  737.                    AllDone(FALSE);
  738.                 }
  739.             }
  740.             break;
  741.         case PUSH:
  742.             if (sp >= STACK_SIZE)
  743.                 Fatal("stack overflow");
  744.             stack[sp].h = h;
  745.             stack[sp].v = v;
  746.             stack[sp].w = w;
  747.             stack[sp].x = x;
  748.             stack[sp].y = y;
  749.             stack[sp].z = z;
  750.             sp++;
  751.             break;
  752.         case POP:
  753.             --sp;
  754.             if (sp < 0)
  755.                 Fatal("stack underflow");
  756.             h = stack[sp].h;
  757.             v = stack[sp].v;
  758.             w = stack[sp].w;
  759.             x = stack[sp].x;
  760.             y = stack[sp].y;
  761.             z = stack[sp].z;
  762.             break;
  763.         case RIGHT1:
  764.         case RIGHT2:
  765.         case RIGHT3:
  766.         case RIGHT4:
  767.             val = SignExtend(dvifp, (int) command - RIGHT1 + 1);
  768.             if (Emitting)
  769.                 MoveOver(val);
  770.             break;
  771.         case W0:
  772.             if (Emitting)
  773.                 MoveOver(w);
  774.             break;
  775.         case W1:
  776.         case W2:
  777.         case W3:
  778.         case W4:
  779.             w = SignExtend(dvifp, (int)command - W1 + 1);
  780.             if (Emitting)
  781.                 MoveOver(w);
  782.             break;
  783.         case X0:
  784.             if (Emitting)
  785.                 MoveOver(x);
  786.             break;
  787.         case X1:
  788.         case X2:
  789.         case X3:
  790.         case X4:
  791.             x = SignExtend(dvifp, (int)command - X1 + 1);
  792.             if (Emitting)
  793.                 MoveOver(x);
  794.             break;
  795.         case DOWN1:
  796.         case DOWN2:
  797.         case DOWN3:
  798.         case DOWN4:
  799.             val = SignExtend(dvifp, (int)command - DOWN1 + 1);
  800.             if (Emitting)
  801.                 MoveDown(val);
  802.             break;
  803.         case Y0:
  804.             if (Emitting)
  805.                 MoveDown(y);
  806.             break;
  807.         case Y1:
  808.         case Y2:
  809.         case Y3:
  810.         case Y4:
  811.             y = SignExtend(dvifp, (int)command - Y1 + 1);
  812.             if (Emitting)
  813.                 MoveDown(y);
  814.             break;
  815.         case Z0:
  816.             if (Emitting)
  817.                 MoveDown(z);
  818.             break;
  819.         case Z1:
  820.         case Z2:
  821.         case Z3:
  822.         case Z4:
  823.             z = SignExtend(dvifp, (int)command - Z1 + 1);
  824.             if (Emitting)
  825.                 MoveDown(z);
  826.             break;
  827.         case FNT1:
  828.         case FNT2:
  829.         case FNT3:
  830.         case FNT4:
  831.             if (!SkipMode) {
  832.                 SetFntNum(NoSignExtend(dvifp, (int)command -FNT1 +1),Emitting);
  833.             }
  834.             break;
  835.         case XXX1:
  836.         case XXX2:
  837.         case XXX3:
  838.         case XXX4:
  839.             k = (int) NoSignExtend(dvifp, (int)command - XXX1 + 1);
  840.             GetBytes(dvifp, SpecialStr, k);
  841.             if (Emitting)
  842.                 DoSpecial(SpecialStr, k);
  843.             break;
  844.         case FNT_DEF1:
  845.         case FNT_DEF2:
  846.         case FNT_DEF3:
  847.         case FNT_DEF4:
  848.             k = (int) NoSignExtend(dvifp, (int)command - FNT_DEF1 + 1);
  849.             SkipFontDef();    /* SkipFontDef(k); */
  850.             break;
  851.         case PRE:
  852.             Fatal("PRE occurs within file");
  853.             break;
  854.         case POST:
  855.             AllDone(FALSE);
  856.             PassNo = 0;
  857.             break;
  858.         case POST_POST:
  859.             Fatal("POST_POST with no preceding POST");
  860.             break;
  861.         default:
  862.             if (command >= FONT_00 && command <= FONT_63) {
  863.                 if (!SkipMode)
  864.                     SetFntNum((long) command - FONT_00, Emitting);
  865.             } else if (command >= SETC_000 && command <= SETC_127) {
  866.                 if (!SkipMode) {
  867.                     SetString(command, PassNo);
  868.                 }
  869.             } else
  870.                 Fatal("%d is an undefined command", command);
  871.             break;
  872.         }
  873.     } /* while TRUE */
  874. }
  875.  
  876.  
  877. /*-->ActualFactor*/
  878. /**********************************************************************/
  879. /**************************  ActualFactor  ****************************/
  880. /**********************************************************************/
  881. double  /* compute the actual size factor given the approximation */
  882. ActualFactor(unmodsize)
  883. long    unmodsize;                 /* actually factor * 1000 */
  884. {
  885.     double  realsize;     /* the actual magnification factor */
  886.     realsize = (double)unmodsize / 1000.0;
  887.     if (abs((int)(unmodsize - 1095l))<2)
  888.         realsize = 1.095445115; /*stephalf*/
  889.     else if (abs((int)(unmodsize - 1315l))<2)
  890.         realsize = 1.31453414; /*stepihalf*/
  891.     else if (abs((int)(unmodsize - 1577l))<2)
  892.         realsize = 1.57744097; /*stepiihalf*/
  893.     else if (abs((int)(unmodsize - 1893l))<2)
  894.         realsize = 1.89292916; /*stepiiihalf*/
  895.     else if (abs((int)(unmodsize - 2074l))<2)
  896.         realsize = 2.0736;   /*stepiv*/
  897.     else if (abs((int)(unmodsize - 2488l))<2)
  898.         realsize = 2.48832;  /*stepv*/
  899.     else if (abs((int)(unmodsize - 2986l))<2)
  900.         realsize = 2.985984; /*stepvi*/
  901.     /* the remaining magnification steps are represented with sufficient
  902.        accuracy already */
  903.     return(realsize);
  904. }
  905.  
  906.  
  907. /*-->AllDone*/
  908. /**********************************************************************/
  909. /****************************** AllDone  ******************************/
  910. /**********************************************************************/
  911. void
  912. AllDone(PFlag)
  913. bool PFlag;
  914. {
  915.     double  time;
  916.  
  917.     if (DoublePage && (PageParity==1)) { /* Shall we go around again?*/
  918.         int k,rc;
  919.         char    n[STRSIZE];
  920.  
  921.         if (EvenPage && Reverse) {
  922.             WouldPrint ++;
  923.             qfprintf(stderr,"[EvenPage] ");
  924.             FormFeed();
  925.         }
  926. #ifdef LJ
  927.         Reverse = (bool)!Reverse;
  928. #endif
  929.         if (Reverse) {
  930.             if (!PFlag) {
  931.                fseek (dvifp, postambleptr, 0);
  932.                (void) NoSignExtend(dvifp, 1);
  933.                ppagep = NoSignExtend(dvifp, 4);
  934.             }
  935.             fseek(dvifp, ppagep, 0);
  936.         } else {
  937.             fseek(dvifp,  14l, 0);
  938.         k = (int) NoSignExtend(dvifp, 1);
  939.         GetBytes(dvifp, n, k);
  940.         }
  941.  
  942.  
  943. #ifdef LJ
  944.         EMIT(outfp, "\033&l2H"); /* Manual Feed */
  945. #endif
  946.         qfprintf(stderr,"\n----------------------starting second pass\n");
  947.  
  948. #ifdef IBM3812
  949.         PMPout(6,"\326\001\305\300\326\252");
  950.         /* set display; ring bell; stop; clear display */
  951.         PMPflush;
  952. #endif
  953.         if (Reverse) {
  954.         if (ZeroPage) WouldPrint++;
  955.         if ((WouldPrint%2) == 1) {
  956.             qfprintf(stderr,"[Padding] ");
  957.             FormFeed();
  958.         }
  959.         }
  960.         WouldPrint = 0;
  961.         if (!Reverse && ZeroPage) {
  962.         WouldPrint++;
  963.         qfprintf(stderr,"[ZeroPage] ");
  964.         FormFeed();
  965.         }
  966.         PageParity = 0;
  967.         PrintPages = StartPrintPages;
  968.         return;
  969.     }
  970.  
  971.     if (EvenPage && DoublePage && !Reverse) WouldPrint++;
  972.  
  973.     if (DoublePage) {
  974.         if (Reverse) {
  975.         if (ZeroPage) {
  976.             WouldPrint ++;
  977.             qfprintf(stderr,"[ZeroPage] ");
  978.             FormFeed();
  979.         }
  980.         }
  981.         else if ((WouldPrint%2) != 0) {
  982.             qfprintf(stderr,"[Padding] ");
  983.             FormFeed();
  984.         }
  985.     }
  986.  
  987. #ifdef IBM3812
  988.     PMPout(10, "\373\010PMP.init");  /* re-init printer  */
  989.     PMPflush;
  990.  
  991.     if (used_fontstorage > MAXFONTSTORAGE) {
  992.         Warning("\n\7used font_storage of %s: %ld Bytes (of %ld)\7",
  993.             PRINTER, used_fontstorage, MAXFONTSTORAGE);
  994.         Warning("Try to format file in separate runs!");
  995.     } else
  996.         qfprintf(stderr,
  997.            "\nAll done, used font_storage of %s: %ld Bytes (of %ld)",
  998.              PRINTER, used_fontstorage, MAXFONTSTORAGE);
  999. #endif
  1000. #ifdef LJ
  1001.     qfprintf(stderr, "\nAll done, used font_storage of %s: %ld Bytes",
  1002.              PRINTER, used_fontstorage);
  1003.     EMIT(outfp, "\033E");
  1004.     if (PrintTestPage) EMIT(outfp, "\033z");
  1005. #endif
  1006.  
  1007.     if (!G_quiet) {
  1008.         fprintf(stderr,"\nDynamically allocated storage: %ld Bytes \n",
  1009.                     allocated_storage);
  1010.         fprintf(stderr,"%d characters downloaded as soft fonts\n", G_ncdl);
  1011.  
  1012. #ifdef TIMING
  1013.         ftime(&timebuffer);
  1014.         time = ((timebuffer.time) + (timebuffer.millitm) / 1000.0) -start_time;
  1015.  
  1016.         if (ndone > 0) {
  1017.             fprintf(stderr,
  1018.            "Time of complete run: %.2f seconds, %d page(s), %.2f seconds/page",
  1019.                       time, ndone, time / ndone);
  1020.             fprintf(stderr," (%.2f ppm)\n",(ndone * 60) / time);
  1021.         }
  1022.         fprintf(stderr,"\n");
  1023. #endif
  1024.     }
  1025.  
  1026.     exit(G_errenc);
  1027. }
  1028.  
  1029.  
  1030. /*-->CopyFile*/   /* copy a file straight through to output */
  1031. /*********************************************************************/
  1032. /***************************** CopyFile ******************************/
  1033. /*********************************************************************/
  1034. void
  1035. CopyFile( str )
  1036. char    *str;
  1037. {
  1038.     FILE    * spfp;
  1039.     char    t;
  1040.     if ( (spfp = BINOPEN(str)) == NULL ) {
  1041.         Warning("Unable to open file %s", str );
  1042.         return;
  1043.     }
  1044.     qfprintf(stderr," [%s", str);
  1045.     for (t = (char)getc(spfp); !feof(spfp); t = (char)getc(spfp))
  1046.         putc(t, outfp);
  1047.     fclose(spfp);
  1048.     qfprintf(stderr,"]");
  1049. }
  1050.  
  1051.  
  1052. /*-->DecodeArgs*/
  1053. /*********************************************************************/
  1054. /***************************** DecodeArgs ****************************/
  1055. /*********************************************************************/
  1056. void
  1057. DecodeArgs( argc, argv )
  1058. int     argc;
  1059. char    *argv[];
  1060. {
  1061.     int     argind;            /* argument index for flags      */
  1062.     char    curarea[STRSIZE];  /* current file area             */
  1063.     char    curname[STRSIZE];  /* current file name             */
  1064.     char    *tcp, *tcp1;       /* temporary character pointers  */
  1065.     char    *this_arg;
  1066.     double  x_offset=0.0, y_offset=0.0;
  1067.  
  1068.     if ((tcp = getenv("TEXPXL")) != NULL) PXLpath = tcp;
  1069.  
  1070.     argind = 1;
  1071.     while (argind < argc) {
  1072.         tcp = argv[argind];
  1073.         if (*tcp == '-') {
  1074.             ++tcp;
  1075.             switch (*tcp) {
  1076.             case 'a':       /* a selects different pxl font area */
  1077.                 PXLpath = ++tcp;
  1078.                 break;
  1079. #ifdef IBM3812
  1080.             case 'b':       /* first page from alternate casette */
  1081.                 FirstAlternate = TRUE;
  1082.                 break;
  1083. #endif
  1084.             case 'c':       /* number of copies to print */
  1085.                 if ( sscanf(tcp + 1, "%hd", &ncopies) != 1 )
  1086.                    Fatal("Argument of -c is not a valid integer\n");
  1087.                 if (ncopies<1) {
  1088.                   Warning("argument of -c < 1; set to 1!");
  1089.                   ncopies=1;
  1090.                 }
  1091.                 break;
  1092. #ifdef DEBUG
  1093.             case 'd':       /* d selects Debug output */
  1094.                 Debug = TRUE;
  1095.                 break;
  1096. #endif
  1097.             case 'D':       /* D selects DoublePage  */
  1098.                 DoublePage = TRUE;
  1099.                 break;
  1100.  
  1101.             case 'e':       /* emit file is specified */
  1102.                 EmitFileName = ++tcp;
  1103. #ifdef MSDOS
  1104.                 /* delete trailing ':' (causing hangup) */
  1105.                 if (EmitFileName[strlen(EmitFileName)-1] == ':')
  1106.                     EmitFileName[strlen(EmitFileName)-1] = '\0';
  1107. #endif
  1108. #ifdef OS2  /* repeated to avoid problems with stupid c preprocessors  */
  1109.                 /* delete trailing ':' (causing hangup) */
  1110.                 if (EmitFileName[strlen(EmitFileName)-1] == ':')
  1111.                     EmitFileName[strlen(EmitFileName)-1] = '\0';
  1112. #endif
  1113.                 break;
  1114.             case 'f':       /* next arg is starting pagenumber */
  1115.                 if ( sscanf(tcp + 1, "%ld", &FirstPage) != 1 )
  1116.                     Fatal("Argument is not a valid integer\n");
  1117.                 break;
  1118. #ifdef LJ
  1119.             case 'g':       /* do not reset printer (go) */
  1120.                 ResetPrinter = FALSE;
  1121.                 break;
  1122. #endif
  1123.             case 'h':     /* copy header file through to output  */
  1124.                 HeaderFileName = ++tcp;
  1125.                 G_header = TRUE;
  1126.                 break;
  1127. #if defined(LJ2P) || defined(IBM3812)
  1128.             case 'l':       /* landscape  */
  1129.                 Landscape = TRUE;
  1130.                 break;
  1131. #endif
  1132.             case 'x':       /* specify x-offset */
  1133.                 this_arg = 0;
  1134.                 if (!(*++tcp)) {
  1135.                     this_arg = (++argind >= argc ? 0 : argv[argind]);
  1136.                 } else {
  1137.                     this_arg = tcp;
  1138.                 }
  1139.                 if (!this_arg
  1140.                      || sscanf(this_arg,"%lf", &x_offset) != 1)
  1141.          Fatal("Argument of -x is not a valid floating point number\n");
  1142.                 break;
  1143.             case 'y':       /* specify y-offset */
  1144.                 this_arg = 0;
  1145.                 if (!(*++tcp)) {
  1146.                     this_arg = (++argind >= argc ? 0 : argv[argind]);
  1147.                 } else {
  1148.                     this_arg = tcp;
  1149.                 }
  1150.                 if (!this_arg || sscanf(this_arg, "%lf", &y_offset) != 1)
  1151.                 Fatal("Argument of -y is not a valid floating point number\n");
  1152.                 break;
  1153.             case 'X':       /* specify X-origin in dots */
  1154.                 this_arg = 0;
  1155.                 if (!(*++tcp)) {
  1156.                     this_arg = (++argind >= argc ? 0 : argv[argind]);
  1157.                 } else {
  1158.                     this_arg = tcp;
  1159.                 }
  1160.                 if (!this_arg || sscanf(this_arg,"%hd", &x_origin) != 1)
  1161.                    Fatal("Argument of -X is not a valid integer\n");
  1162.                 break;
  1163.             case 'Y':       /* specify Y-origin in dots */
  1164.                 this_arg = 0;
  1165.                 if (!(*++tcp)) {
  1166.                     this_arg = (++argind >= argc ? 0 : argv[argind]);
  1167.                 } else {
  1168.                     this_arg = tcp;
  1169.                 }
  1170.                 if (!this_arg ||
  1171.                      sscanf(this_arg, "%hd", &y_origin) != 1)
  1172.                         Fatal("Argument of -Y is not a valid integer\n");
  1173.                 break;
  1174.             case 'm':       /* specify magnification to use */
  1175.                 switch ( (*++tcp) ) {
  1176.                 case '#':
  1177.                      /* next arg is a magnification to use */
  1178.                     if ( sscanf(tcp + 1, "%ld", &usermag) != 1 )
  1179.                        Fatal("Argument of mag is not a valid integer\n");
  1180.                     break;
  1181.                 case '0':
  1182.                     usermag = 1000;
  1183.                     break;
  1184.                 case 'h':
  1185.                 case 'H':
  1186.                     usermag = 1095;
  1187.                     break;
  1188.                 case '1':
  1189.                     usermag = 1200;
  1190.                     break;
  1191.                 case 'q':
  1192.                     usermag = 1250;
  1193.                     break;
  1194.                 case '2':
  1195.                     usermag = 1440;
  1196.                     break;
  1197.                 case '3':
  1198.                     usermag = 1728;
  1199.                     break;
  1200.                 case '4':
  1201.                     usermag = 2074;
  1202.                     break;
  1203.                 case '5':
  1204.                     usermag = 2488;
  1205.                     break;
  1206.                 default:
  1207.                     Fatal("%c is a bad mag step\n", *tcp);
  1208.                 }
  1209.                 break;
  1210. #ifdef SUPERCOMMENT
  1211.             case 'o':     /* PostScript command to send */
  1212.                 if ( ++argind >= argc )
  1213.                     Fatal("No argument following -o\n", 0);
  1214.                 PScmd[nps++] = argv[argind];
  1215.                 break;
  1216. #endif
  1217.             case 'p':       /* print n pages  */
  1218.                 if ( sscanf(tcp + 1, "%ld", &PrintPages) != 1 )
  1219.                     Fatal("Argument is not a valid integer\n");
  1220.                 if (PrintPages < 1)
  1221.                   Fatal("Argument of -p must be greater than 0\n");
  1222.                 break;
  1223.             case 'q':       /* quiet operation */
  1224.                 G_quiet = TRUE;
  1225.                 break;
  1226.             case 'r':       /* switch order to process pages */
  1227.                 Reverse = (bool)(!Reverse);
  1228.                 break;
  1229. #ifdef LJ
  1230.             case 's':       /* specify X-origin in dots */
  1231.                 this_arg = 0;
  1232.                 if (!(*++tcp)) this_arg = (++argind >= argc ? 0 : argv[argind]);
  1233.                 else           this_arg = tcp;
  1234.                 if (!this_arg || sscanf(this_arg,"%hd", &pagesize) != 1)
  1235.                    Fatal("Argument of -s is not a valid integer\n");
  1236.                 switch (pagesize) {
  1237.                 case 1: pgsiz_dots = 3150; break;         /* executive */
  1238.                 case 2: pgsiz_dots = 3300; break;         /* letter */
  1239.                 case 3: pgsiz_dots = 4200; break;         /* legal */
  1240.                 case 26: pgsiz_dots = 3507; break;        /* a4 */
  1241.                 case 80: pgsiz_dots = 2250; break;        /* monarc */
  1242.                 case 81: pgsiz_dots = 2850; break;        /* com10 */
  1243.                 case 90: pgsiz_dots = 2598; break;        /* int dl */
  1244.                 case 91: pgsiz_dots = 2704; break;        /* int c5 */
  1245.                 default: Fatal(
  1246.                    "%hd is a bad value for pagesize (1,2,3,26,80,81,90,91)",
  1247.                    pagesize);
  1248.                 }
  1249.                 break;
  1250. #endif
  1251.             case 't':       /* ending pagenumber */
  1252.                 if ( sscanf(tcp + 1, "%ld", &LastPage) != 1 )
  1253.                     Fatal("Argument is not a valid integer\n");
  1254.                 break;
  1255.             case 'v':    /* verbatim mode (print pxl-file names) */
  1256.                 G_noverbatim = FALSE;
  1257.                 break;
  1258.             case 'w':       /* don't print out warnings */
  1259.                 G_nowarn = TRUE;
  1260.                 break;
  1261. #ifdef LJ
  1262.             case 'z':
  1263.                 PrintTestPage = (bool)(!PrintTestPage);
  1264.                 break;
  1265. #endif
  1266.             default:
  1267.                 fprintf(stderr, "%c is not a legal flag\n", *tcp);
  1268.             }
  1269.         } else {
  1270.  
  1271.             (void) strcpy(filename, tcp);
  1272.             tcp = strrchr(argv[argind], '/');
  1273.             /* split into directory + file name */
  1274.             if (tcp == NULL)  {
  1275.                 curarea[0] = '\0';
  1276.                 tcp = argv[argind];
  1277.             } else {
  1278.                 (void) strcpy(curarea, argv[argind]);
  1279.                 curarea[tcp-argv[argind]+1] = '\0';
  1280.                 tcp += 1;
  1281.             }
  1282.  
  1283.             (void) strcpy(curname, tcp);
  1284.             /* split into file name + extension */
  1285.             tcp1 = strchr(tcp, '.');
  1286.             if (tcp1 == NULL) {
  1287.                 (void) strcpy(rootname, curname);
  1288.                 strcat(curname, ".dvi");
  1289.             } else {
  1290.                 *tcp1 = '\0';
  1291.                 (void) strcpy(rootname, curname);
  1292.                 *tcp1 = '.';
  1293.             }
  1294.  
  1295.             (void) strcpy(filename, curarea);
  1296.             (void) strcat(filename, curname);
  1297.  
  1298.             if ((dvifp = BINOPEN(filename)) == NULL)  {
  1299.                 Fatal("%s: can't find DVI file \"%s\"\n\n",
  1300.                        G_progname, filename);
  1301.             }
  1302.         }
  1303.         argind++;
  1304.     }
  1305.  
  1306.     x_goffset = (short) MM_TO_PXL(x_offset) + x_origin;
  1307.     y_goffset = (short) MM_TO_PXL(y_offset) + y_origin;
  1308.  
  1309.     if (dvifp == NULL)  {
  1310.       fprintf(stderr,"\nThis is the DVI to %s converter version %s",
  1311.           PRINTER, VERSION);
  1312.       fprintf(stderr," (%s)\n", OS);
  1313.       fprintf(stderr,"usage: %s [OPTIONS] dvifile\n", G_progname);
  1314.  
  1315.       fprintf(stderr,"OPTIONS are:\n");
  1316.       fprintf(stderr,
  1317.         "\t-aX ..... X= searchpath leading to pixel-files (.pk or .pxl)\n");
  1318. #ifdef IBM3812
  1319.       fprintf(stderr,
  1320.         "\t-b  ..... take paper for first page from alternate casette\n");
  1321. #endif
  1322.       fprintf(stderr,"\t-cX ..... X= number of copies\n");
  1323. #ifdef DEBUG
  1324.       fprintf(stderr,"\t-d  ..... turns debug output on\n");
  1325. #endif
  1326.       fprintf(stderr,"\t-D  ..... turns doublepage output on\n");
  1327.       fprintf(stderr,"\t-eX ..... X= output file\n");
  1328.       fprintf(stderr,"\t-fX ..... print from begin of page number X\n");
  1329. #ifdef LJ
  1330.       fprintf(stderr,
  1331.           "\t-g  ..... do not reset printer at begin of job (go)\n");
  1332. #endif
  1333.       fprintf(stderr,"\t-hX ..... X= name of headerfile\n");
  1334. #ifdef LJ2P
  1335.       fprintf(stderr,"\t-l  ..... landscape mode\n");
  1336. #endif
  1337.       fprintf(stderr,"\t-mX ..... magnification X=0;h;1;2;3;4;5;#xxxx\n");
  1338.       fprintf(stderr,"\t-pX ..... print X pages\n");
  1339.       fprintf(stderr,"\t-q  ..... quiet operation\n");
  1340.       fprintf(stderr,"\t-r  ..... process pages in reverse order\n");
  1341. #ifdef LJ
  1342.       fprintf(stderr,"\t-sX ..... set paper size to X (see documentation)\n");
  1343. #endif
  1344.       fprintf(stderr,"\t-tX ..... print to end of page number X\n");
  1345.       fprintf(stderr,"\t-w  ..... don't print out warnings\n");
  1346.       fprintf(stderr,"\t-v  ..... tell user which pixel-files are used\n");
  1347.       fprintf(stderr,"\t-xX ..... X= x-offset on printout in mm\n");
  1348.       fprintf(stderr,"\t-yX ..... X= y-offset on printout in mm\n");
  1349.       fprintf(stderr,"\t-XO ..... O= x page origin in dots (default=%d)\n",
  1350.             XDEFAULTOFF );
  1351.       fprintf(stderr,"\t-YO ..... O= y page origin in dots (default=%d)\n",
  1352.             YDEFAULTOFF );
  1353. #ifdef LJ
  1354.       fprintf(stderr,
  1355.           "\t-z  ..... print test page with pagecounter after job\n");
  1356. #endif
  1357.       exit(1);
  1358.     }
  1359.     if (EQ(EmitFileName, "")) {
  1360.         if ((EmitFileName = (char *)malloc( STRSIZE )) != (void *)NULL)
  1361.             allocated_storage += STRSIZE;
  1362.         else
  1363.             Fatal("Can't allocate storage of %d bytes\n",STRSIZE);
  1364.         (void) strcpy(EmitFileName, curname);
  1365.         tcp1 = strchr(EmitFileName, '.');
  1366.         *tcp1 = '\0';
  1367.         strcat(EmitFileName, EMITFILE_EXTENSION);
  1368.     }
  1369. }
  1370.  
  1371.  
  1372. /*-->DoConv*/
  1373. /*********************************************************************/
  1374. /********************************  DoConv  ***************************/
  1375. /*********************************************************************/
  1376. long
  1377. DoConv(num, den, convResolution)
  1378. long    num, den;
  1379. int     convResolution;
  1380. {
  1381.     /*register*/ double conv;
  1382.     double foo, bar, blat;
  1383.  
  1384.     foo  = ((double)num / (double)den);
  1385.     bar  = ((double)mag / 1000.0);
  1386.     blat = ((double)convResolution/254000.0);
  1387.     conv = foo * bar * blat;
  1388.  
  1389. /*** too complex an expression for compiler
  1390.     conv = ((double)num / (double)den) *
  1391.             ((double)mag / 1000.0) *
  1392.           ((double)convResolution/254000.0);
  1393. ***/
  1394.  
  1395.     return((long) ((1.0/conv)+0.5));
  1396. }
  1397.  
  1398.  
  1399. /*-->DoSpecial*/
  1400. /*********************************************************************/
  1401. /*****************************  DoSpecial  ***************************/
  1402. /*********************************************************************/
  1403. typedef enum  {
  1404.     None, String, Integer, Number, Dimension}
  1405.  
  1406.  
  1407. ValTyp;
  1408. typedef struct {
  1409.     char    *Key;       /* the keyword string */
  1410.     char    *Val;       /* the value string */
  1411.     ValTyp  vt;         /* the value type */
  1412.     union {         /* the decoded value */
  1413.         int     i;
  1414.         float   n;
  1415.     } v;
  1416. } KeyWord;
  1417. typedef struct {
  1418.     char    *Entry;
  1419.     ValTyp  Typ;
  1420. } KeyDesc;
  1421. #define PSFILE 0
  1422. #define ORIENTATION 1
  1423. #define RESETPOINTS 2
  1424. #define DEFPOINT 3
  1425. #define FILL 4
  1426. #define GRAY 5
  1427. #define PATTERN 6
  1428. KeyDesc KeyTab[] = {
  1429.     { "file", String },
  1430.     { "orientation", Integer},
  1431.     { "resetpoints", String},
  1432.     { "defpoint", String},
  1433.     { "fill", String},
  1434.     { "gray", Integer},
  1435.     { "pattern", Integer}
  1436.    /*,
  1437.              {"hsize", Dimension},
  1438.              {"vsize", Dimension},
  1439.              {"hoffset", Dimension},
  1440.              {"voffset", Dimension},
  1441.              {"hscale", Number},
  1442.              {"vscale", Number}*/
  1443.  
  1444.  
  1445. };
  1446.  
  1447.  
  1448. #define NKEYS (sizeof(KeyTab)/sizeof(KeyTab[0]))
  1449. void
  1450. DoSpecial( str, n )
  1451. /* interpret a \special command, made up of keyword=value pairs */
  1452. char    *str;
  1453. int     n;
  1454. {
  1455. char    spbuf[STRSIZE], xs[STRSIZE], ys[STRSIZE];
  1456. char    *sf = NULL;
  1457. float   x,y;
  1458. long     x_pos, y_pos;
  1459. KeyWord k;
  1460. int     i, j, j1;
  1461. static int GrayScale=10, Pattern=1;
  1462. static bool    GrayFill=TRUE;
  1463. static long    p_x[80], p_y[80];
  1464. str[n] = '\0';
  1465. spbuf[0] = '\0';
  1466.  
  1467. SetPosn(h, v);
  1468. while ( (str = GetKeyStr(str, &k)) != NULL ) {
  1469.     /* get all keyword-value pairs */
  1470.     /* for compatibility, single words are taken as file names */
  1471.     if ( k.vt == None && access(k.Key, 0) == 0) {
  1472.         if ( sf )
  1473.             Warning("More than one \\special file name given. %s ignored", sf);
  1474.         (void) strcpy(spbuf, k.Key);
  1475.         sf = spbuf;
  1476. /*
  1477.         for (j = 1; ((sf[j]=='/' ? sf[j]='\\':sf[j]) != '\0'); j++);
  1478. */
  1479.     } else if ( GetKeyVal( &k, KeyTab, NKEYS, &i ) && i != -1 )
  1480.         switch (i) {
  1481.         case PSFILE:
  1482.             if ( sf )
  1483.                 Warning("More than one \\special file name given. %s ignored",
  1484.                             sf);
  1485.             (void) strcpy(spbuf, k.Val);
  1486.             sf = spbuf;
  1487. /*
  1488.             for (j=1; ((sf[j]=='/' ? sf[j]='\\':sf[j]) != '\0'); j++) ;
  1489. */
  1490.             break;
  1491.         case ORIENTATION:
  1492. #ifdef IBM3812
  1493.             if ((k.v.i >= 0) && (k.v.i < 4)) {
  1494.                 last_ry = UNKNOWN;
  1495.                 sprintf(PMPformat, "\322%c", (unsigned char)k.v.i);
  1496.                 PMPout(2, PMPformat);
  1497.         if (k.v.i == 1) Landscape = TRUE;
  1498.         else if (k.v.i == 0) Landscape = FALSE;
  1499. #endif
  1500. #ifdef LJ
  1501.             if ((k.v.i >= 0) && (k.v.i < 2)) {
  1502.                 last_ry = UNKNOWN;
  1503.                 EMIT(outfp, "\033&l%dO\033*rF", (unsigned char)k.v.i);
  1504. #endif
  1505.             } else
  1506.                 Warning( "Invalid orientation (%d)given; ignored.", k.v.i);
  1507.             break;
  1508.         case RESETPOINTS:
  1509.             (void) strcpy(spbuf, k.Val);
  1510.  
  1511.             sf = NULL;
  1512.             break;
  1513.  
  1514.         case DEFPOINT:
  1515.           (void) strcpy(spbuf, k.Val);
  1516.            i=sscanf(spbuf,"%d(%[^,],%s)",&j,xs,ys);
  1517.            if (i>0) {
  1518.               x_pos=h; y_pos=v;
  1519.               if (i>1) {
  1520.                   if (sscanf(xs,"%fpt",&x)>0) {
  1521.                      fprintf(stderr,"x = %f\n",x);
  1522.                      x_pos = PT_TO_DVI(x);
  1523.              }
  1524.               }
  1525.               if (i>2) {
  1526.                   if (sscanf(ys,"%fpt",&y)>0) {
  1527.                      fprintf(stderr,"y = %f\n",y);
  1528.                      y_pos = PT_TO_DVI(y);
  1529.                 }
  1530.               }
  1531.               p_x[j]=x_pos;
  1532.               p_y[j]=y_pos;
  1533.            } else
  1534.              Warning("invalid point definition\n");
  1535.  
  1536.            sf = NULL;
  1537.            break;
  1538.  
  1539.         case FILL:
  1540.           (void) strcpy(spbuf, k.Val);
  1541.            i=sscanf(spbuf,"%d/%d %s",&j,&j1,xs);
  1542.            if (i>1) {
  1543. #ifdef LJ
  1544.               SetPosn(p_x[j], p_y[j]);
  1545.               x_pos = (long)PIXROUND(p_x[j1]-p_x[j], hconv);
  1546.               y_pos = (long)PIXROUND(p_y[j1]-p_y[j], vconv);
  1547.               if (labs(x_pos)<labs(y_pos)) x_pos=x_pos+3;
  1548.               else                         y_pos=y_pos+3;
  1549.               if (GrayFill) {
  1550.                   EMIT(outfp, "\033*c%lda%ldb%dg2P", x_pos, y_pos, GrayScale);
  1551.               } else {
  1552.                   EMIT(outfp, "\033*c%lda%ldb%dg3P", x_pos, y_pos, Pattern);
  1553.               }
  1554.               last_ry = UNKNOWN;
  1555. #endif
  1556.            }
  1557.            break;
  1558.         case GRAY:
  1559.             if ((k.v.i >= 0) && (k.v.i < 101)) {
  1560.                 GrayScale = k.v.i;
  1561.                 GrayFill = TRUE;
  1562.             } else
  1563.                 Warning( "Invalid gray scale (%d) given; ignored.", k.v.i);
  1564.             break;
  1565.         case PATTERN:
  1566.             if ((k.v.i >= 0) && (k.v.i < 7)) {
  1567.                 Pattern = k.v.i;
  1568.                 GrayFill = FALSE;
  1569.             } else
  1570.                 Warning( "Invalid pattern (%d) given; ignored.", k.v.i);
  1571.             break;
  1572.  
  1573.         default:
  1574.             Warning("Can't handle %s=%s command; ignored.", k.Key, k.Val);
  1575.             break;
  1576.         }
  1577.     else
  1578.         Warning("Invalid keyword or value in \\special - <%s> ignored", k.Key);
  1579. }
  1580. if ( sf ) {
  1581.     last_ry = UNKNOWN;
  1582. #ifdef IBM3812
  1583.     PMPflush;
  1584. #endif
  1585.     CopyFile( sf );
  1586.    }
  1587. }
  1588.  
  1589.  
  1590. /*-->EmitChar*/
  1591. /**********************************************************************/
  1592. /****************************  EmitChar  ******************************/
  1593. /**********************************************************************/
  1594. void                     /* output a character bitmap */
  1595. EmitChar(c, ce)
  1596. long    c;
  1597. struct char_entry *ce;
  1598. {
  1599.     register int    i;
  1600.     register unsigned char  *sl;
  1601.     unsigned short  nbpl, nwpl;
  1602.     long    total;
  1603. #ifdef LJ
  1604.     unsigned char cnv_buffer[10];
  1605. #endif
  1606.  
  1607. /*
  1608. #ifdef LJ
  1609. printf("Emit character %c(%d) id=%d, yoff=%d[%d], w=%d[%d], h=%d[%d]\n",
  1610.         (char)c,(int)c,
  1611.     fontptr->plusid,
  1612.     ce->yOffset, fontptr->max_yoff,
  1613.     ce->width,   fontptr->max_width,
  1614.     ce->height,  fontptr->max_height
  1615. );
  1616. #endif
  1617. */
  1618.  
  1619.     if ( fontptr->ncdl == 0 ) {
  1620. #ifdef IBM3812
  1621.         used_fontstorage += 1028;
  1622. #endif
  1623. #ifdef LJ
  1624.         if (fontptr->max_width == 0) { /* we have no realistic values */
  1625.                 fontptr->max_yoff = CHAR_HEIGTH_LARGE;
  1626.                 fontptr->max_width = CHAR_WIDTH_LARGE;
  1627.                 fontptr->max_height = CHAR_HEIGTH_LARGE*2;
  1628.         }
  1629.  
  1630.         /* open font dict before first char, set active font */
  1631.         INT_ASCII(cnv_buffer,fontptr->plusid);
  1632.         EMIT(outfp, "\033*c%sD\033)s26W", cnv_buffer);
  1633. /*      EMITB(20, "\0\032\0\1\0\0\0\310\0\377\0\377\0\1\1\025\0\4\0\4");*/
  1634.  
  1635.         EMITB(6, "\0\032\0\2\0\0");        /* GUGUGU 255 */
  1636.         EMITWORD( fontptr->max_yoff);
  1637.         EMITWORD( fontptr->max_width);
  1638.         EMITWORD( fontptr->max_height);
  1639. /*        EMITC((char)(Landscape==TRUE));  */
  1640.         EMITC((char)0);
  1641.         EMITB(7, "\1\1\25\0\4\0\4");
  1642.         EMITB(6, "\0\0\0\0\0\0");
  1643.         EMIT(outfp, "\033*c4F");
  1644. #endif
  1645.     }
  1646.     if ( fontptr != prevfont ) {   /* because this isn't done on pass 0 */
  1647. #ifdef LJ
  1648.         INT_ASCII(cnv_buffer,fontptr->plusid);
  1649.         EMIT(outfp, "\033(%sX", cnv_buffer);
  1650. #endif
  1651.         prevfont = fontptr;
  1652.     }
  1653.  
  1654. #ifdef USEPXL
  1655.     if (fontptr->id == pk89)   {
  1656.         nbpl = ((ce->width) +  7) >> 3;
  1657.         total = (long)ce->height * nbpl;
  1658.     } else if (fontptr->id == id1002)   {
  1659.         nbpl = ((ce->width) +  7) >> 3;
  1660.         total = (long)ce->height * nbpl;
  1661.     } else if (fontptr->id == id1001) {
  1662.         nwpl = ((ce->width) + 31) >> 5;
  1663.         nbpl = ((ce->width) + 7) >> 3;
  1664.         total = (long)ce->height * nbpl;
  1665.     }
  1666. #else
  1667.     nbpl = (num_cols + 7) >> 3;
  1668.     total = num_rows * nbpl;
  1669. #endif
  1670. /***************************************************************
  1671.     if ( ((char) c == 'i') && (fontptr->plusid == 0)) {
  1672.         long j;
  1673.         printf("cols=%ld, ncols=%ld\n",nwpl,nbpl);
  1674.  
  1675.         printf("%ld Bytes:->",total);
  1676.         for (j=0; j<total;j++) {
  1677.             char *ch; char ic;
  1678.             ch = (char *)(ce->where.address.pixptr);
  1679.             ic = *(ch+j);
  1680.             printf("%X.",ic);
  1681.                 }
  1682.         printf("<- Now Emitting\n");
  1683.         }
  1684. ***************************************************************/
  1685. #ifdef USEPXL
  1686. #ifdef IBM3812
  1687.     if ((short)(ce->height) - ce->yOffset > 55) {
  1688.         ce->yyOffset = (short) ce->height - ce->yOffset;
  1689.         ce->yOffset  = (short) ce->height;
  1690.     } else {
  1691.         ce->yyOffset = (short) 0;
  1692.     }
  1693. #endif
  1694. #ifdef LJ
  1695.         ce->yyOffset = (short) 0;
  1696. #endif
  1697. #endif
  1698.  
  1699.     /* ce->cw = (long)(((double)ce->tfmw / (double)hconv) +0.5); */
  1700.     /* set active font to nn, load font pattern  xx ... */
  1701. #ifdef IBM3812
  1702.     PMPcont(total + 9);
  1703. #ifdef USEPXL
  1704.     sprintf(PMPformat, "\323%c\360%c%c%c",
  1705.         (unsigned char)fontptr->plusid,
  1706.         (unsigned char)c,
  1707.         (unsigned char)ce->height,
  1708.         (unsigned char)ce->width);
  1709.     PMPout(6, PMPformat);
  1710.     PMPoutC((char)(-(ce->xOffset)));
  1711.     PMPoutC((char)(ce->cw - (-ce->xOffset + ce->width)));
  1712.     PMPoutC((char)(ce->yOffset));
  1713. #else
  1714.     sprintf(PMPformat, "\323%c\360%c%c%c",
  1715.         (unsigned char)fontptr->plusid,
  1716.         (unsigned char)c,
  1717.         (unsigned char)num_rows,
  1718.         (unsigned char)num_cols);
  1719.     PMPout(6, PMPformat);
  1720.     PMPoutC((char)(-x_offset));
  1721.     PMPoutC((char)(ce->cw - (-x_offset + num_cols)));
  1722.     PMPoutC((char)num_rows-y_offset);
  1723. #endif
  1724. #endif
  1725. #ifdef LJ
  1726.     INT_ASCII(cnv_buffer,fontptr->plusid);
  1727.     EMIT(outfp, "\033*c%sd%dE\033(s%ldW", cnv_buffer,
  1728.         (unsigned int)c, (long)(total + 16));
  1729.     EMITB(4, "\4\0\016\1");
  1730. /*    EMITC((char)(Landscape==TRUE)); */
  1731.     EMITC((char)0);
  1732.     EMITC((char)0);
  1733. #ifdef USEPXL
  1734.     EMITWORD(-ce->xOffset);
  1735.     EMITWORD(ce->yOffset);
  1736.     EMITWORD(ce->width);
  1737.     EMITWORD(ce->height);
  1738. #else
  1739.     EMITWORD(-x_offset);
  1740.     EMITWORD(num_rows-y_offset);
  1741.     EMITWORD(num_cols);
  1742.     EMITWORD(num_rows);
  1743. #endif
  1744.     EMITWORD((int)ce->cw * 4);
  1745. #endif
  1746. #ifdef USEPXL
  1747.  
  1748.     if (fontptr->id == pk89)
  1749.         PkRaster(ce, FALSE);
  1750.     else if (fontptr->id == id1002)
  1751.         for (i = 0; i < (int) ce->height; i++) {
  1752.             sl = ((unsigned char *)(ce->where.address.pixptr) + i * nbpl);
  1753.             EMITL(nbpl, sl);
  1754.         }
  1755.     else if (fontptr->id == id1001)
  1756.         for (i = 0; i < (int) ce->height; i++) {
  1757.             sl = (unsigned char *)(ce->where.address.pixptr + i * nwpl);
  1758.             EMITL(nbpl, sl);
  1759.         }
  1760. #else
  1761.     for (i = num_rows; i > 0; i--)
  1762.       {
  1763.         EMITL (nbpl, bits + (i-1) * nbpl);
  1764.       }
  1765. #endif
  1766. #ifdef IBM3812
  1767. #ifdef USEPXL
  1768.     used_fontstorage += (long)ce->height * ((ce->width + 15) >> 4)
  1769.         *2 + 14;
  1770. #else
  1771.     used_fontstorage += (long)num_rows * ((num_cols + 15) >> 4)
  1772.         *2 + 14;
  1773. #endif
  1774. #endif
  1775. #ifdef LJ
  1776. #ifdef USEPXL
  1777.     used_fontstorage += 64 * ((((ce->height * ce->width) - 1) / 64) + 1);
  1778. #else
  1779.     used_fontstorage += 64 * ((((num_rows * num_cols) - 1) / 64) + 1);
  1780. #endif
  1781. #endif
  1782.     fontptr->ncdl += 1;
  1783.     G_ncdl += 1;
  1784. }
  1785.  
  1786.  
  1787. /*-->RasterLine*/
  1788. /**********************************************************************/
  1789. /****************************  RasterLine  ****************************/
  1790. /**********************************************************************/
  1791. void
  1792. RasterLine(ce, nbpl, current_line, buffer)
  1793. struct char_entry *ce;
  1794. unsigned short  nbpl, current_line;
  1795. char    *buffer;
  1796. {
  1797. #ifdef IBM3812
  1798.     long    total;
  1799.     static unsigned short   maxlines;
  1800.  
  1801.     if (current_line == 0) {
  1802. #ifdef USEPXL
  1803.         maxlines = ce->height;
  1804.  
  1805.         MoveVert(-ce->yOffset);      /* move cursor up */
  1806.         MoveHor(-ce->xOffset);       /* move cursor right */
  1807. #else
  1808.         maxlines = num_rows;
  1809.  
  1810.         MoveVert(-(num_rows-y_offset));      /* move cursor up */
  1811.         MoveHor(-x_offset);       /* move cursor right */
  1812. #endif
  1813.         last_ry = UNKNOWN;       /* next time full positioning */
  1814.     }
  1815.  
  1816.     if (current_line % maxlines == 0) {
  1817.         if (current_line > 0) {    /* maxlines lines have been printed*/
  1818.             MoveVert(maxlines);   /*   move cursor down     */
  1819.             last_ry = UNKNOWN;    /* next time full positioning */
  1820.         }
  1821. #ifdef USEPXL
  1822.         total = (long)(ce->height - current_line) * (long)nbpl;
  1823. #else
  1824.         total = (long)(num_rows - current_line) * (long)nbpl;
  1825. #endif
  1826.         if ((total + 9) > 65535) {
  1827.             maxlines = (unsigned short)((65535 - 9) / nbpl);
  1828.             total = (long)maxlines * (long)nbpl;
  1829.         }
  1830.  
  1831.         PMPcont(total + 9);
  1832.         PMPout(2, "\365\0");
  1833.         EMITWORD(maxlines);
  1834. #ifdef USEPXL
  1835.         EMITWORD(ce->width);
  1836. #else
  1837.         EMITWORD(num_cols);
  1838. #endif
  1839.         PMPoutC((unsigned char) (total >> 16) & 0xFF);
  1840.         PMPoutC((unsigned char) (total >> 8 ) & 0xFF);
  1841.         PMPoutC((unsigned char)  total     & 0xFF);
  1842.     }
  1843.     EMITL((int)nbpl, buffer);
  1844. #endif
  1845. #ifdef LJ
  1846.     register int    emitbytes;
  1847.  
  1848.     for (emitbytes = (int)nbpl;
  1849.         (*(buffer + emitbytes - 1) == '\0') && (emitbytes > 0);
  1850.         emitbytes--) ;
  1851.     EMIT(outfp, "\033*b%dW", emitbytes);
  1852.     EMITL(emitbytes, buffer);
  1853. #endif
  1854. }
  1855.  
  1856.  
  1857. /*-->RasterChar*/
  1858. /**********************************************************************/
  1859. /****************************  RasterChar  ****************************/
  1860. /**********************************************************************/
  1861. void                     /* raster a character bitmap */
  1862. RasterChar(ce)
  1863. struct char_entry *ce;
  1864. {
  1865.     int     i;
  1866.     register unsigned char  *sl;
  1867.     unsigned short  nbpl, nwpl;
  1868.     unsigned char   raster_line_buf[BYTES_PER_PIXEL_LINE];
  1869.  
  1870. #ifdef DEBUG
  1871.     if (Debug)
  1872.         fprintf(stderr,"Raster character ...size=%d \n", (int)ce->charsize );
  1873. #endif
  1874.  
  1875. #ifdef USEPXL
  1876.     if (fontptr->id == pk89)   {
  1877.         nbpl = ((ce->width) +  7) >> 3;
  1878.     } else if (fontptr->id == id1002)   {
  1879.         nbpl = ( ce->width +  7) >> 3;
  1880.     } else if (fontptr->id == id1001) {
  1881.         nwpl = ( ce->width + 31) >> 5;
  1882.         nbpl = ( ce->width + 7) >> 3;
  1883.     }
  1884. #else
  1885.     nbpl = (num_cols + 7) >> 3;
  1886. #endif
  1887.  
  1888. #ifdef LJ
  1889.     EMIT(outfp, "\033*t300R\033*r1A");
  1890. #endif
  1891.  
  1892.                                             { /* read pixel from file */
  1893.     if ((ce->charsize == HUGE_SIZE) && (fontptr->id != pk89))
  1894.         OpenFontFile();
  1895. #ifdef USEPXL
  1896.         fseek(pxlfp, ce->where.address.fileOffset, 0);
  1897. #else
  1898.         fseek(gfin, ce->where.address.fileOffset, 0);
  1899.         gettochar();
  1900.         readbits();
  1901. #endif
  1902.     }
  1903.  
  1904. #ifdef USEPXL
  1905.     if (fontptr->id == pk89)
  1906.         PkRaster(ce, TRUE);
  1907.     else if (fontptr->id == id1002) {
  1908.         for (i = 0; i < (int) ce->height; i++) {
  1909.             if (ce->charsize == HUGE_SIZE) {
  1910.                 fread(raster_line_buf, 1, (int) nbpl, pxlfp);
  1911.                 sl = raster_line_buf;
  1912.             } else
  1913.                 sl = ((unsigned char *)(ce->where.address.pixptr)
  1914.                     + i * nbpl);
  1915.  
  1916.             RasterLine(ce, (unsigned int)nbpl, i, sl);
  1917.         }
  1918.     } else if (fontptr->id == id1001) {
  1919.         long    filediff;
  1920.         filediff = (long)nwpl * 4 - nbpl;
  1921.         for (i = 0; i < (int) ce->height; i++) {
  1922.             if (ce->charsize == HUGE_SIZE) {
  1923.                 fread(raster_line_buf, 1, (int) nbpl, pxlfp);
  1924.                 /* skip fill bytes */
  1925.                 fseek(pxlfp, filediff, 1);
  1926.                 sl = raster_line_buf;
  1927.             } else
  1928.                 sl = (unsigned char *)(ce->where.address.pixptr + i * nwpl);
  1929.  
  1930.             RasterLine(ce, (unsigned int)nbpl, i, sl);
  1931.         }
  1932.     }
  1933. #else
  1934.     for (i = num_rows; i > 0; i--)
  1935.       RasterLine(ce, (unsigned int)nbpl, i, bits + (i-1) * nbpl);
  1936. #endif
  1937. #ifdef LJ
  1938.     EMIT(outfp, "\033*rB");
  1939. #endif
  1940.     last_ry = UNKNOWN;
  1941. }
  1942.  
  1943.  
  1944. /*-->Fatal*/
  1945. /**********************************************************************/
  1946. /******************************  Fatal  *******************************/
  1947. /**********************************************************************/
  1948. void
  1949. Fatal(fmt, a, b, c)      /* issue a fatal error message */
  1950. char    *fmt;           /* format              */
  1951. char    *a, *b, *c;     /* arguments           */
  1952. {
  1953.     fprintf(stderr, "\n");
  1954.     fprintf(stderr, "%s: FATAL--", G_progname);
  1955.     fprintf(stderr, fmt, a, b, c);
  1956.     fprintf(stderr, "\n\n");
  1957.     exit(2);
  1958. }
  1959.  
  1960.  
  1961. /*-->FindPostAmblePtr*/
  1962. /**********************************************************************/
  1963. /************************  FindPostAmblePtr  **************************/
  1964. /**********************************************************************/
  1965. void
  1966. FindPostAmblePtr(postambleptr)
  1967. long    *postambleptr;
  1968. /* this routine will move to the end of the file and find the start
  1969.     of the postamble */
  1970. {
  1971.     long    i;
  1972.     fseek (dvifp,  0l, 2);   /* goto end of file */
  1973.     *postambleptr = ftell (dvifp) - 4;
  1974.     fseek (dvifp, *postambleptr, 0);
  1975.     while (TRUE) {
  1976.         fseek (dvifp, --(*postambleptr), 0);
  1977.         if (((i = NoSignExtend(dvifp, 1)) != 223) &&
  1978.             (i != DVIFORMAT))
  1979.             Fatal ("Bad end of DVI file");
  1980.         if (i == DVIFORMAT)
  1981.             break;
  1982.     }
  1983.     fseek (dvifp, (*postambleptr) - 4, 0);
  1984.     (*postambleptr) = NoSignExtend(dvifp, 4);
  1985.     fseek (dvifp, *postambleptr, 0);
  1986. }
  1987.  
  1988.  
  1989. /*-->GetBytes*/
  1990. /**********************************************************************/
  1991. /*****************************  GetBytes  *****************************/
  1992. /**********************************************************************/
  1993. void
  1994. GetBytes(fp, cp, n)     /* get n bytes from file fp */
  1995. /*register*/ FILE *fp;      /* file pointer  */
  1996. register char   *cp;    /* character pointer */
  1997. int     n;            /* number of bytes  */
  1998. {
  1999.     while (n--)
  2000.         *cp++ = (char)getc(fp);
  2001. }
  2002.  
  2003.  
  2004. /*-->GetFontDef*/
  2005. /**********************************************************************/
  2006. /**************************** GetFontDef  *****************************/
  2007. /**********************************************************************/
  2008. void
  2009. GetFontDef()
  2010. /***********************************************************************
  2011.    Read the font  definitions as they  are in the  postamble of the  DVI
  2012.    file.
  2013. ***********************************************************************/
  2014. {
  2015.     unsigned char   byte;
  2016.     while (((byte = (unsigned char) NoSignExtend(dvifp, 1)) >= FNT_DEF1) &&
  2017.         (byte <= FNT_DEF4)) {
  2018.         switch (byte) {
  2019.         case FNT_DEF1:
  2020.             ReadFontDef ( NoSignExtend(dvifp, 1));
  2021.             break;
  2022.         case FNT_DEF2:
  2023.             ReadFontDef ( NoSignExtend(dvifp, 2));
  2024.             break;
  2025.         case FNT_DEF3:
  2026.             ReadFontDef ( NoSignExtend(dvifp, 3));
  2027.             break;
  2028.         case FNT_DEF4:
  2029.             ReadFontDef ( NoSignExtend(dvifp, 4));
  2030.             break;
  2031.         default:
  2032.             Fatal ("Bad byte value in font defs");
  2033.             break;
  2034.         }
  2035.     }
  2036.     if (byte != POST_POST)
  2037.         Fatal ("POST_POST missing after fontdefs");
  2038. }
  2039.  
  2040.  
  2041. /*-->GetKeyStr*/
  2042. /**********************************************************************/
  2043. /*****************************  GetKeyStr  ****************************/
  2044. /**********************************************************************/
  2045. /* extract first keyword-value pair from string (value part may be null)
  2046.  * return pointer to remainder of string
  2047.  * return NULL if none found
  2048.  */
  2049. char    KeyStr[STRSIZE];
  2050. char    ValStr[STRSIZE];
  2051. char
  2052. *GetKeyStr( str, kw )
  2053. char    *str;
  2054. KeyWord *kw;
  2055. {
  2056.     char    *s, *k, *v, t;
  2057.     if ( !str )
  2058.         return( NULL );
  2059.     for ( s = str; *s == ' '; s++)
  2060.         ;          /* skip over blanks */
  2061.     if ( *s == '\0' )
  2062.         return( NULL );
  2063.     for ( k = KeyStr; /* extract keyword portion */
  2064.     *s != ' ' && *s != '\0' && *s != '=';
  2065.         *k++ = *s++)
  2066.         ;
  2067.     *k = '\0';
  2068.     kw->Key = KeyStr;
  2069.     kw->Val = v = NULL;
  2070.     kw->vt = None;
  2071.     for ( ; *s == ' '; s++)
  2072.         ;            /* skip over blanks */
  2073.     if ( *s != '=' )         /* look for "=" */
  2074.         return( s );
  2075.     for ( s++; *s == ' '; s++);      /* skip over blanks */
  2076.     if ( *s == '\'' || *s == '\"' )  /* get string delimiter */
  2077.         t = *s++;
  2078.     else
  2079.         t = ' ';
  2080.     for ( v = ValStr; /* copy value portion up to delim */
  2081.     *s != t && *s != '\0';
  2082.         *v++ = *s++)
  2083.         ;
  2084.     if ( t != ' ' && *s == t )
  2085.         s++;
  2086.     *v = '\0';
  2087.     kw->Val = ValStr;
  2088.     kw->vt = String;
  2089.     return( s );
  2090. }
  2091.  
  2092.  
  2093. /*-->GetKeyVal*/
  2094. /**********************************************************************/
  2095. /*****************************  GetKeyVal  ****************************/
  2096. /**********************************************************************/
  2097. /* get next keyword-value pair decode value according to table entry  */
  2098. bool
  2099. GetKeyVal( kw, tab, nt, tno)
  2100. KeyWord *kw;
  2101. KeyDesc tab[];
  2102. int     nt;
  2103. int     *tno;
  2104. {
  2105.     int     i;
  2106.     char    c = '\0';
  2107.     *tno = -1;
  2108.     for (i = 0; i < nt; i++)
  2109.         if ( IsSame(kw->Key, tab[i].Entry) ) {
  2110.             *tno = i;
  2111.             switch ( tab[i].Typ ) {
  2112.             case None:
  2113.                 if ( kw->vt != None )
  2114.                     return( FALSE );
  2115.                 break;
  2116.             case String:
  2117.                 if ( kw->vt != String )
  2118.                     return( FALSE );
  2119.                 break;
  2120.             case Integer:
  2121.                 if ( kw->vt != String )
  2122.                     return( FALSE );
  2123.                 if ( sscanf(kw->Val, "%d%c",
  2124.                     &(kw->v.i), &c) != 1
  2125.                      || c != '\0' )
  2126.                     return( FALSE );
  2127.                 break;
  2128. /*              case Number:
  2129.  *              case Dimension:
  2130.  *                  if( kw->vt != String ) return( FALSE );
  2131.  *                  if( sscanf(kw->Val,"%f%c",
  2132.  *                     &(kw->v.n), &c) != 1
  2133.  *                  || c != '\0' ) return( FALSE );
  2134.  *                  break;
  2135.  */
  2136.             }
  2137.             kw->vt = tab[i].Typ;
  2138.             return( TRUE );
  2139.         }
  2140.     return( TRUE );
  2141. }
  2142.  
  2143.  
  2144.  
  2145. /*-->IsSame*/
  2146. /**********************************************************************/
  2147. /*******************************  IsSame  *****************************/
  2148. /**********************************************************************/
  2149. bool                  /* compare strings, ignore case */
  2150. IsSame(a, b)
  2151. char    *a, *b;
  2152. {
  2153.     char    *x, *y;
  2154.     x = a;
  2155.     y = b;
  2156.     for ( ; *a != '\0'; )
  2157.         if ( tolower(*a++) != tolower(*b++) )
  2158.             return( FALSE );
  2159.     return( *x == *y ? TRUE : FALSE );
  2160. }
  2161.  
  2162.  
  2163. /*-->NoSignExtend*/
  2164. /**********************************************************************/
  2165. /***************************  NoSignExtend  ***************************/
  2166. /**********************************************************************/
  2167. long
  2168. NoSignExtend(fp, n)     /* return n byte quantity from file fd */
  2169. register FILE *fp;      /* file pointer    */
  2170. register int    n;      /* number of bytes */
  2171. {
  2172.     long    x;      /* number being constructed */
  2173.     x = 0;
  2174.     while (n--)  {
  2175.         x <<= 8;
  2176.         x |= getc(fp);
  2177.     }
  2178.     return(x);
  2179. }
  2180.  
  2181.  
  2182. /*-->OpenFontFile*/
  2183. /**********************************************************************/
  2184. /************************** OpenFontFile  *****************************/
  2185. /**********************************************************************/
  2186. void
  2187. OpenFontFile()
  2188. /***********************************************************************
  2189.     The original version of this dvi driver reopened the font file  each
  2190.     time the font changed, resulting in an enormous number of relatively
  2191.     expensive file  openings.   This version  keeps  a cache  of  up  to
  2192.     MAXOPEN open files,  so that when  a font change  is made, the  file
  2193.     pointer, pxlfp, can  usually be  updated from the  cache.  When  the
  2194.     file is not found in  the cache, it must  be opened.  In this  case,
  2195.     the next empty slot  in the cache  is assigned, or  if the cache  is
  2196.     full, the least used font file is closed and its slot reassigned for
  2197.     the new file.  Identification of the least used file is based on the
  2198.     counts of the number  of times each file  has been "opened" by  this
  2199.     routine.  On return, the file pointer is always repositioned to  the
  2200.     beginning of the file.
  2201. ***********************************************************************/
  2202. {
  2203.     int     i, least_used, current;
  2204.     struct pixel_list tmp;
  2205.  
  2206. #ifdef DEBUG
  2207.     if (Debug)
  2208.         fprintf(stderr,"open font file %d\n", fontptr->font_file_id);
  2209. #endif
  2210. /*
  2211. fprintf(stderr,"? %lx == %lx\n", pfontptr,fontptr);
  2212. */
  2213.     if ((pfontptr == fontptr) && (pxlfp != NO_FILE))
  2214.         return;         /* we need not have been called */
  2215.  
  2216.        if (fontptr->font_file_id == NO_FILE)
  2217.         return;         /* we need not have been called */
  2218.  
  2219.     tmp = pixel_files[1];
  2220.     for (current = 1;
  2221.         (current <= nopen) && (tmp.pixel_file_id != fontptr->font_file_id); ) {
  2222.         ++current;
  2223.         tmp = pixel_files[current];
  2224.     }
  2225.     /* try to find file in open list */
  2226.  
  2227.     if (current <= nopen)       /* file already open */ {
  2228.         if ( (pxlfp = pixel_files[current].pixel_file_id) != NO_FILE )
  2229.             fseek(pxlfp, 0l, 0);
  2230.             /* reposition to start of file */
  2231.     } else {
  2232.             /* file not in open list          */
  2233.         if (nopen < MAXOPEN)    /* just add it to list    */
  2234.             current = ++nopen;
  2235.         else  {
  2236.             /* list full -- find least used file,     */
  2237.             /* close it, and reuse slot for new file  */
  2238.             least_used = 1;
  2239.             for (i = 2; i <= MAXOPEN; ++i)
  2240.                 if (pixel_files[least_used].use_count>pixel_files[i].use_count)
  2241.                     least_used = i;
  2242.             if (pixel_files[least_used].pixel_file_id != NO_FILE) {
  2243.                 FILE * fid;
  2244.                 struct font_entry *fp;
  2245.                 fid = pixel_files[least_used].pixel_file_id;
  2246.                 /* mark file as being closed in the entry */
  2247.                 fp = hfontptr;
  2248.                 while (fp != NULL && fp->font_file_id != fid) fp = fp->next;
  2249.                 if (fp == NULL)
  2250.                    Fatal("Open file %x not found in font entry list.\n", fid);
  2251.                 else {
  2252.                     fp->font_file_id = NULL;
  2253.                 }
  2254.                 fclose( fid );
  2255.             }
  2256. #ifdef DEBUG
  2257.             if (Debug)
  2258.                  fprintf(stderr,"\n__reuse slot %d\n", least_used);
  2259. #endif
  2260.             current = least_used;
  2261.         }
  2262.         if ((pxlfp = BINOPEN(fontptr->name)) == NULL) {
  2263.             Warning("PXL-file %s could not be opened", fontptr->name);
  2264.             pxlfp = NO_FILE;
  2265.         } else {
  2266. #ifdef DEBUG
  2267.              if (Debug) fprintf(stderr,
  2268.              "Opening File  <%s> /%x/, Size(font_entry)=%d\n",
  2269.               fontptr->name, pxlfp, sizeof(struct font_entry ));
  2270. #endif
  2271.  
  2272.         }
  2273.         pixel_files[current].pixel_file_id = pxlfp;
  2274.         pixel_files[current].use_count = 0;
  2275.     }
  2276.     pfontptr = fontptr;         /* make previous = current font */
  2277.     fontptr->font_file_id = pxlfp;      /* set file identifier */
  2278.     pixel_files[current].use_count++;   /* update reference count */
  2279. #ifndef USEPXL
  2280.     gfin = pxlfp;
  2281. #endif
  2282. }
  2283.  
  2284.  
  2285. /*-->PixRound*/
  2286. /**********************************************************************/
  2287. /*****************************  PixRound  *****************************/
  2288. /**********************************************************************/
  2289. long
  2290. PixRound(x, conv)       /* return rounded number of pixels */
  2291. long    x;          /* in DVI units     */
  2292. long    conv;       /* conversion factor */
  2293. {
  2294.     return((x + conv) / conv);
  2295. }
  2296.  
  2297.  
  2298. /*-->ReadFontDef*/
  2299. /**********************************************************************/
  2300. /****************************  ReadFontDef  ***************************/
  2301. /**********************************************************************/
  2302. void
  2303. ReadFontDef(k)
  2304. long    k;
  2305. {
  2306.     long    t;
  2307.     unsigned short i;
  2308.     char    nname[STRSIZE];
  2309.     int nmag;
  2310.     struct font_entry *tfontptr; /* temporary font_entry pointer   */
  2311.     struct char_entry *tcharptr; /* temporary char_entry pointer  */
  2312.     static int      plusid = 0;
  2313.     bool font_found = FALSE;
  2314. #ifdef LJ
  2315.     int depth, max_depth;
  2316. #endif
  2317.  
  2318. #ifdef DEBUG
  2319.     if (Debug)
  2320.         fprintf(stderr,"Mallocating %d Bytes)...\n",
  2321.             sizeof(struct font_entry ));
  2322. #endif
  2323.  
  2324.     if ((tfontptr = NEW(struct font_entry )) == NULL)
  2325.         Fatal("can't malloc space for font_entry");
  2326.  
  2327.     allocated_storage += sizeof(struct font_entry );
  2328.  
  2329.     tfontptr->next = hfontptr;
  2330.     tfontptr->font_file_id = NULL;
  2331.     fontptr = hfontptr = tfontptr;
  2332.     tfontptr->ncdl = 0;
  2333.     tfontptr->k = k;
  2334.     tfontptr->c = NoSignExtend(dvifp, 4); /* checksum */
  2335.     tfontptr->s = NoSignExtend(dvifp, 4); /* space size */
  2336.     tfontptr->d = NoSignExtend(dvifp, 4); /* design size */
  2337.     tfontptr->a = (int) NoSignExtend(dvifp, 1); /* length for font name */
  2338.     tfontptr->l = (int) NoSignExtend(dvifp, 1); /* device length */
  2339.  
  2340. #ifdef LJ
  2341.     tfontptr->max_width = tfontptr->max_height = tfontptr->max_yoff =
  2342.                           max_depth = 0;
  2343. #endif
  2344.  
  2345.     GetBytes(dvifp, tfontptr->n, tfontptr->a + tfontptr->l);
  2346.     tfontptr->n[tfontptr->a+tfontptr->l] = '\0';
  2347.  
  2348.     tfontptr->font_mag = (long)((
  2349.          ActualFactor((long)(1000.0*tfontptr->s/(double)tfontptr->d+0.5))
  2350.          * ActualFactor(mag)
  2351. #ifdef USEPXL
  2352.          * RESOLUTION * 5.0
  2353. #else
  2354.          * RESOLUTION
  2355. #endif
  2356.            ) + 0.5);
  2357. /*
  2358. printf("[%ld]=%lf * %lf * %lf + 0.5 = %ld\n",
  2359.     ((long)(1000.0*tfontptr->s/(double)tfontptr->d+0.5)),
  2360.     ActualFactor((long)(1000.0*tfontptr->s/(double)tfontptr->d+0.5)),
  2361.     ActualFactor(mag),
  2362.     (double)RESOLUTION * 5,
  2363.     tfontptr->font_mag );
  2364. */
  2365.  
  2366.  
  2367.     if (!(font_found = (bool)
  2368.           findfile(PXLpath, tfontptr->n, tfontptr->font_mag,
  2369.           tfontptr->name))) {
  2370.           Warning(tfontptr->name); /* contains error messsage */
  2371.           tfontptr->font_file_id = NO_FILE;
  2372.         }
  2373.     else if (!( (G_noverbatim) || (G_quiet) ) )
  2374.         fprintf(stderr,"%d: using font <%s>\n",
  2375.         plusid,tfontptr->name);
  2376.  
  2377.     tfontptr->plusid = plusid;
  2378.     plusid++;
  2379.  
  2380.     /* sprintf(tfontptr->psname,"%s.%ld.%d",
  2381.        tfontptr->n,tfontptr->font_mag,tfontptr->plusid);*/
  2382.  
  2383. #ifdef LJ
  2384.     if (plusid >= HANDLE_MAX_FONTS)
  2385.         Fatal("can handle only %d fonts! ask a wizzard..\n",
  2386.                HANDLE_MAX_FONTS);
  2387. #endif
  2388.     if (tfontptr != pfontptr) {
  2389.         if (font_found) OpenFontFile();
  2390.         else
  2391.             pxlfp = NO_FILE;
  2392.     }
  2393. #ifdef USEPXL
  2394.     if ( pxlfp == NO_FILE ) {        /* allow missing pxl files */
  2395.         tfontptr->magnification = 0;
  2396.         tfontptr->designsize = 0;
  2397. #endif
  2398.         for (i = FIRSTFNTCHAR; i <= LASTFNTCHAR; i++) {
  2399.             tcharptr = &(tfontptr->ch[i]);
  2400. #ifdef USEPXL
  2401.             tcharptr->width = 0;
  2402.             tcharptr->height = 0;
  2403.             tcharptr->xOffset = 0;
  2404.             tcharptr->yOffset = 0;
  2405. #endif
  2406.             tcharptr->where.isloaded = FALSE;
  2407.             tcharptr->where.address.fileOffset = NONEXISTANT;
  2408.             tcharptr->tfmw = 0;
  2409.         }
  2410. #ifdef USEPXL
  2411.         return;
  2412.     }
  2413.     t = (long) NoSignExtend(pxlfp, 1);
  2414.     if (t == 0) {
  2415.         t = (long) NoSignExtend(pxlfp, 1);
  2416.         t = (long) NoSignExtend(pxlfp, 2);
  2417.         if (t == 1002)
  2418.             tfontptr->id = id1002;
  2419.         else if (t == 1001)
  2420.             tfontptr->id = id1001;
  2421.         else
  2422.             Fatal("Unknown Version of PXL-format\n");
  2423.     } else {
  2424.         if (t == PK_PRE)    {
  2425.             unsigned char   temp_byte;
  2426.             temp_byte = (unsigned char) NoSignExtend(pxlfp, 1);
  2427.             if (temp_byte != PK_ID) Fatal(
  2428.                "Wrong Version of pk file!  (%d should be 89)\n",
  2429.                              (int)temp_byte);
  2430.             else
  2431.                 tfontptr->id = pk89;
  2432.         } else
  2433.             Fatal("unknown font format in file <%s> !\n",fontptr->name);
  2434.     }
  2435.  
  2436.     if ((tfontptr->id == id1002) || (tfontptr->id == id1001)) {
  2437.         fseek(pxlfp, -20l, 2);
  2438.  
  2439.         t = NoSignExtend(pxlfp, 4);
  2440.         if ((tfontptr->c != 0) && (t != 0) && (tfontptr->c != t))
  2441.     Warning("font = \"%s\",\n->tfm checksum = %lX,\n->pxl checksum = %lX",
  2442.                           tfontptr->name, tfontptr->c, t);
  2443.         tfontptr->magnification = NoSignExtend(pxlfp, 4);
  2444.         tfontptr->designsize    = NoSignExtend(pxlfp, 4);
  2445.  
  2446.         if (tfontptr->id == id1001)
  2447.             fseek(pxlfp, (long) (NoSignExtend(pxlfp, 4) * 4), 0);
  2448.         else
  2449.             fseek(pxlfp, (long) NoSignExtend(pxlfp, 4) , 0);
  2450.  
  2451.         for (i = FIRSTFNTCHAR; i <= LASTFNTCHAR; i++) {
  2452.             tcharptr = &(tfontptr->ch[i]);
  2453.             tcharptr->width   = (unsigned short) NoSignExtend(pxlfp, 2);
  2454.             tcharptr->height  = (unsigned short) NoSignExtend(pxlfp, 2);
  2455.             tcharptr->xOffset = (short) SignExtend(pxlfp, 2);
  2456.             tcharptr->yOffset = (short) SignExtend(pxlfp, 2);
  2457.             tcharptr->where.isloaded = FALSE;
  2458.             if (tfontptr->id == id1001)
  2459.                 tcharptr->where.address.fileOffset = NoSignExtend(pxlfp,4) * 4;
  2460.             else
  2461.                 tcharptr->where.address.fileOffset = NoSignExtend(pxlfp,4);
  2462.             tcharptr->tfmw = (long)
  2463.             (   (double)(NoSignExtend(pxlfp, 4))
  2464.               * (double)tfontptr->s / (double) 0x100000 );
  2465.             tcharptr->cw = (long)(((double)tcharptr->tfmw/(double)hconv) + 0.5);
  2466.  
  2467.             if (tcharptr->width  > CHAR_WIDTH_LARGE  ||
  2468.                 tcharptr->height > CHAR_HEIGTH_LARGE )
  2469.                 tcharptr->charsize = LARGE_SIZE;
  2470.             else
  2471.                 tcharptr->charsize = SMALL_SIZE;
  2472. #ifdef LJ
  2473.             max(tfontptr->max_width,tcharptr->width);
  2474.             max(tfontptr->max_height,tcharptr->height);
  2475.             if (tcharptr->yOffset > 0)
  2476.                 max(tfontptr->max_yoff,tcharptr->yOffset);
  2477.             if ((depth = tcharptr->height - tcharptr->yOffset)>max_depth)
  2478.                    max_depth = depth;
  2479. #endif
  2480.  
  2481.         }
  2482. #ifdef LJ
  2483.         tfontptr->max_height = max_depth ? tfontptr->max_yoff+max_depth :
  2484.                                            tfontptr->max_yoff+1;
  2485. #endif
  2486.     } else { /* PK 89 format */
  2487.         unsigned char   temp_byte;
  2488.         register unsigned char  flag_byte;
  2489.         long    hppp, vppp, pkloc, packet_length, temp;
  2490.         int     car, ii;
  2491.  
  2492.         /* read comment */
  2493.         for ( ii = temp_byte = (unsigned char) NoSignExtend(pxlfp, 1);
  2494.               ii>0; ii--) {
  2495.             flag_byte = (unsigned char) NoSignExtend(pxlfp, 1);
  2496. #ifdef DEBUG
  2497.             if (Debug) fprintf(stderr, "%c", flag_byte ) ;
  2498. #endif
  2499.         }
  2500. #ifdef DEBUG
  2501.         if (Debug) fprintf(stderr, "\n");
  2502. #endif
  2503.         pkloc = 3 + (int)temp_byte;
  2504.         tfontptr->designsize = NoSignExtend(pxlfp, 4);
  2505.  
  2506.         t = NoSignExtend(pxlfp, 4);
  2507.         if ((tfontptr->c != 0) && (t != 0) && (tfontptr->c != t))
  2508.           Warning("font = \"%s\",\n->tfm checksum = %lX,\n->pxl checksum = %lX",
  2509.                  tfontptr->name, tfontptr->c, t);
  2510.  
  2511.         hppp = NoSignExtend(pxlfp, 4);
  2512.         vppp = NoSignExtend(pxlfp, 4);
  2513.         if (hppp != vppp)
  2514.             Warning("aspect ratio is %ld:%ld (should be 1:1)!", hppp,vppp);
  2515.         tfontptr->magnification = (long)(hppp * 72.27 * 5 / 65536l + 0.5);
  2516.  
  2517.         pkloc += 16;
  2518.         flag_byte = skip_specials(&pkloc);
  2519.  
  2520.         while (flag_byte != PK_POST) {
  2521.         if ((flag_byte & 7) == 7) {
  2522.         /* fprintf(stderr,"\nRead long character preamble\n"); */
  2523.  
  2524.            packet_length = (unsigned long)NoSignExtend(pxlfp,4);
  2525.            if ((car = (int)NoSignExtend(pxlfp, 4)) > (LASTFNTCHAR))
  2526.                 Fatal("Bad character (%d) in PK-File\n",(int)car) ;
  2527.  
  2528.            tcharptr = &(tfontptr->ch[car]);
  2529.            tcharptr->where.address.fileOffset = pkloc;
  2530.            /* set pkloc to end_of_packet */
  2531.            pkloc += packet_length + 8;
  2532.  
  2533.            tcharptr->tfmw = (long) NoSignExtend(pxlfp, 4);
  2534.            (void) NoSignExtend(pxlfp, 4); /* horesc not used */
  2535.            (void) NoSignExtend(pxlfp, 4); /* not used */
  2536.  
  2537.            tcharptr ->width   = (unsigned short) NoSignExtend(pxlfp, 4);
  2538.            tcharptr ->height  = (unsigned short) NoSignExtend(pxlfp, 4);
  2539.            tcharptr ->xOffset = (short) SignExtend(pxlfp, 4);
  2540.            tcharptr ->yOffset = (short) SignExtend(pxlfp, 4);
  2541.            tcharptr ->where.isloaded = FALSE;
  2542.         } else if (flag_byte & 4) {
  2543.             /* fprintf(stderr,"Read extended short character preamble\n"); */
  2544.  
  2545.             packet_length = ((long) flag_byte & 3) * 65536l +
  2546.                 (unsigned short) NoSignExtend(pxlfp, 2);
  2547.             if ((car = (int)NoSignExtend(pxlfp, 1)) > (LASTFNTCHAR))
  2548.                 Fatal("Bad character (%d) in PK-File\n",(int)car) ;
  2549.  
  2550.             tcharptr = &(tfontptr->ch[car]);
  2551.             tcharptr->where.address.fileOffset = pkloc;
  2552.             /* set pkloc to end_of_packet */
  2553.             pkloc += packet_length + 3;
  2554.  
  2555.             tcharptr->tfmw = (long) NoSignExtend(pxlfp, 3);
  2556. /*
  2557.             { register unsigned short t;
  2558.               t = (unsigned short) NoSignExtend(pxlfp, 1);
  2559.               tcharptr->tfmw = t * 65536l +
  2560.               (unsigned short) NoSignExtend(pxlfp, 2);
  2561.             }
  2562. */
  2563.             /* horesc not used */
  2564.             (void) NoSignExtend(pxlfp, 2) ;
  2565.             tcharptr ->width   = (unsigned short) NoSignExtend(pxlfp,2);
  2566.             tcharptr ->height  = (unsigned short) NoSignExtend(pxlfp,2);
  2567.             tcharptr ->xOffset = (short) SignExtend(pxlfp, 2);
  2568.             tcharptr ->yOffset = (short) SignExtend(pxlfp, 2);
  2569.             tcharptr ->where.isloaded = FALSE;
  2570.         } else {
  2571.             /* fprintf(stderr,"<Read short character preamble@>\n"); */
  2572.  
  2573.             packet_length = ((long)flag_byte & 3) * 256 +
  2574.                 NoSignExtend(pxlfp, 1) ;
  2575.             if ((car = (int)NoSignExtend(pxlfp, 1)) > (LASTFNTCHAR))
  2576.                 Fatal("Bad character (%d) in PK-File\n",(int)car) ;
  2577.  
  2578.             tcharptr = &(tfontptr->ch[car]);
  2579.             tcharptr->where.address.fileOffset = pkloc;
  2580.             /* set pkloc to end_of_packet */
  2581.             pkloc += packet_length + 2 ;
  2582.  
  2583.             tcharptr->tfmw = (long) NoSignExtend(pxlfp, 3);
  2584. /*
  2585.             { register unsigned short t;
  2586.               t = (unsigned short) NoSignExtend(pxlfp, 1);
  2587.               tcharptr->tfmw = t * 65536l +
  2588.               (unsigned short) NoSignExtend(pxlfp, 2);
  2589.             }
  2590. */
  2591.             /* horesc not used */
  2592.             (void) NoSignExtend(pxlfp, 1) ;
  2593.             tcharptr ->width   = (unsigned short) NoSignExtend(pxlfp,1);
  2594.             tcharptr ->height  = (unsigned short) NoSignExtend(pxlfp,1);
  2595.             tcharptr ->xOffset = (short) SignExtend(pxlfp, 1);
  2596.             tcharptr ->yOffset = (short) SignExtend(pxlfp, 1);
  2597.             tcharptr ->where.isloaded = FALSE;
  2598.         }
  2599.  
  2600.         tcharptr->tfmw = (long)
  2601.            ( tcharptr->tfmw * (double)tfontptr->s / (double) 0x100000 );
  2602.  
  2603.         tcharptr->cw = (long)(((double)tcharptr->tfmw /
  2604.             (double)hconv) + 0.5);
  2605.  
  2606.         if (tcharptr->width  > CHAR_WIDTH_LARGE  ||
  2607.             tcharptr->height > CHAR_HEIGTH_LARGE )
  2608.             tcharptr->charsize = LARGE_SIZE;
  2609.         else
  2610.             tcharptr->charsize = SMALL_SIZE;
  2611.  
  2612. #ifdef LJ
  2613.         max(tfontptr->max_width, tcharptr->width);
  2614.         max(tfontptr->max_height,tcharptr->height);
  2615.         if (tcharptr->yOffset > 0)
  2616.             max(tfontptr->max_yoff,tcharptr->yOffset);
  2617.         if ((depth = tcharptr->height - tcharptr->yOffset) > max_depth)
  2618.                 max_depth = depth;
  2619. #endif
  2620. /*
  2621. fprintf(stderr,"char=%d, tcharptr=%lx, flag_byte=%d, font=%lx\n",car, tcharptr,flag_byte,tfontptr);
  2622. */
  2623.         tcharptr->flag_byte = flag_byte;
  2624.         fseek(pxlfp, (long) pkloc, 0);
  2625.         flag_byte = skip_specials(&pkloc);
  2626.  
  2627.         } /* end of while */
  2628. #ifdef LJ
  2629. tfontptr->max_height = max_depth ? tfontptr->max_yoff+max_depth :
  2630.                                    tfontptr->max_yoff+1;
  2631. #endif
  2632. /*
  2633. printf("fontid=%d: max_width=%u, max_height=%d, max_yoff=%u\n",
  2634.         tfontptr->plusid, tfontptr->max_width,
  2635.         tfontptr->max_height, tfontptr->max_yoff);
  2636. */
  2637. #else
  2638.     if ( pxlfp == NO_FILE )        /* allow missing pxl files */
  2639.     return;
  2640.  
  2641.     gfin = pxlfp;
  2642.     seekpost();
  2643.     readpost();
  2644.     if ((tfontptr->c != 0) && (checksum != 0) && (tfontptr->c != checksum))
  2645.     Warning("font = \"%s\",\n-->font checksum = %d,\n-->dvi checksum = %d",
  2646.         tfontptr->name, tfontptr->c, checksum);
  2647.  
  2648.     for(i=FIRSTFNTCHAR; i<=LASTFNTCHAR; i++) {
  2649.     if (char_exists[i]) {
  2650.         tcharptr = &(tfontptr->ch[i]);
  2651.         tcharptr->tfmw = (long)(((float)tfm_wd[i]*(float)tfontptr->s) /
  2652.            (float)((long)1l<<20));
  2653.         tcharptr->where.address.fileOffset = char_pointer[i];
  2654.       }
  2655. #ifdef LJ
  2656. /*                 GF USER PLEASE CHECK IF THIS CODE WORKS
  2657.     tfontptr->max_width = gf_font_max_m;
  2658.     tfontptr->max_height = gf_font_max_n;
  2659.     tfontptr->max_yoff = gf_font_min_n;
  2660. */
  2661. #endif
  2662. #endif
  2663. /*****************************************************************************/
  2664. /*if (tcharptr->charsize==LARGE_SIZE)                                        */
  2665. /*     printf("%d:\t <%c> w=%d h=%d xO=%d yO=%d tfmw=%ld cw=%ld %d\n",       */
  2666. /*     i,(char) i,                                                           */
  2667. /*     tcharptr->width,tcharptr->height,tcharptr->xOffset,tcharptr->yOffset, */
  2668. /*     tcharptr->tfmw, tcharptr->cw, (int)(tcharptr->charsize));             */
  2669. /*                                                                           */
  2670. /*****************************************************************************/
  2671.     }
  2672.   }
  2673.  
  2674.  
  2675. unsigned char
  2676. skip_specials( pkloc )
  2677. long    *pkloc;
  2678. {
  2679.     long    i, j;
  2680.     register unsigned char  flag_byte;
  2681.     do {
  2682.     flag_byte = (unsigned char) NoSignExtend(pxlfp, 1);
  2683. /*
  2684. fprintf(stderr,"flagbyte = %d, pkloc=%ld\n",(int)flag_byte,*pkloc);
  2685. */
  2686.  
  2687.     (*pkloc) ++;
  2688.     if (flag_byte  >= 240)
  2689.         switch (flag_byte) {
  2690.         case 240:
  2691.         case 241:
  2692.         case 242:
  2693.         case 243 : {
  2694.             i = 0 ;
  2695.             for (j = 240; j <= (long)flag_byte; j++) {
  2696.                 i = 256 * i + NoSignExtend(pxlfp, 1) ;
  2697.                 (*pkloc) ++;
  2698.             }
  2699.             for (j = 1; j <= i; j++) {
  2700.                 (void) NoSignExtend(pxlfp, 1) ;
  2701.                 (*pkloc) ++;
  2702.             }
  2703.             break;
  2704.         }
  2705.         case 244 : {
  2706.             i = NoSignExtend(pxlfp, 4);
  2707.             (*pkloc) += 4;
  2708.             break;
  2709.         }
  2710.         case 245 :
  2711.             break;
  2712.         case 246 :
  2713.             break ;
  2714.         case 247:
  2715.         case 248:
  2716.         case 249:
  2717.         case 250:
  2718.         case 251:
  2719.         case 252:
  2720.         case 253:
  2721.         case 254:
  2722.         case 255: {
  2723.             Fatal("Unexpected flagbyte %d!\n",
  2724.                  (int)flag_byte) ;
  2725.             }
  2726.         }
  2727.     } while (!((flag_byte < 240) || (flag_byte == PK_POST))) ;
  2728.     return(flag_byte);
  2729. }
  2730.  
  2731.  
  2732. /*-->ReadPostAmble*/
  2733. /**********************************************************************/
  2734. /**************************  ReadPostAmble  ***************************/
  2735. /**********************************************************************/
  2736. /***********************************************************************
  2737.     This  routine  is  used  to  read  in  the  postamble  values.    It
  2738.     initializes the magnification and checks  the stack height prior  to
  2739.     starting printing the document.
  2740. ***********************************************************************/
  2741. void
  2742. ReadPostAmble(load)
  2743. bool load;
  2744. {
  2745.     FindPostAmblePtr (&postambleptr);
  2746.     if (NoSignExtend(dvifp, 1) != POST)
  2747.         Fatal ("POST missing at head of postamble");
  2748. #ifdef DEBUG
  2749.     if (Debug)
  2750.         fprintf(stderr,"got POST command\n");
  2751. #endif
  2752.     ppagep = NoSignExtend(dvifp, 4);
  2753.     num = NoSignExtend(dvifp, 4);
  2754.     den = NoSignExtend(dvifp, 4);
  2755.     mag = NoSignExtend(dvifp, 4);
  2756.     if ( usermag > 0 && usermag != mag )
  2757.         Warning("DVI magnification of %ld over-ridden by user (%ld)",
  2758.                      mag, usermag );
  2759.     if ( usermag > 0 )
  2760.         mag = usermag;
  2761.     hconv = DoConv(num, den, hconvRESOLUTION);
  2762.     vconv = DoConv(num, den, vconvRESOLUTION);
  2763.     (void) NoSignExtend(dvifp, 4);   /* height-plus-depth of tallest page */
  2764.     (void) NoSignExtend(dvifp, 4);   /* width of widest page */
  2765.     if (NoSignExtend(dvifp, 2) >= STACK_SIZE)
  2766.         Fatal ("Stack size is too small");
  2767.     (void) NoSignExtend(dvifp, 2);   /* this reads the number of pages in */
  2768.                      /* the DVI file */
  2769. #ifdef DEBUG
  2770.     if (Debug)
  2771.         fprintf(stderr,"now reading font defs");
  2772. #endif
  2773.     if (load)
  2774.         GetFontDef ();
  2775. }
  2776.  
  2777.  
  2778. /*-->LoadAChar*/
  2779. /**********************************************************************/
  2780. /***************************** LoadAChar ******************************/
  2781. /**********************************************************************/
  2782. void
  2783. LoadAChar(c, ptr)
  2784. long    c;
  2785. register struct char_entry *ptr;
  2786. {
  2787.     long    *pr;
  2788.     long    bytes;
  2789.  
  2790.     if (ptr->where.address.fileOffset == NONEXISTANT) {
  2791.         ptr->where.isloaded = FALSE;
  2792.         return;
  2793.     }
  2794.  
  2795.     OpenFontFile();
  2796.  
  2797. #ifdef DEBUG
  2798.     if (Debug)
  2799.       fprintf(stderr, "LoadAChar: <%c>(%ld) from file at pos %ld\n",
  2800.           (char)c,c,ptr->where.address.fileOffset);
  2801. #endif
  2802.  
  2803. #ifdef USEPXL
  2804.  
  2805.     fseek(pxlfp, ptr->where.address.fileOffset, 0);
  2806.  
  2807.     if (fontptr->id == pk89) {
  2808. #ifdef PARANOIA
  2809.         unsigned char   temp;
  2810.         temp = (unsigned char) NoSignExtend(pxlfp, 1);
  2811.  
  2812.         if ((int)(ptr->flag_byte) != (int)temp) {
  2813.            fprintf(stderr,"font=%lx, ptr=%lx\n",fontptr,ptr);
  2814.                 Fatal("%d: ,oh boy! old flag %d, new flag %d, ptr=%lx\n",
  2815.                           (int)c,(int)(ptr->flag_byte),(int)temp,ptr);
  2816.             }
  2817. #endif
  2818.  
  2819.         if ((ptr->flag_byte & 7) == 7) {
  2820.             bytes = ((long) NoSignExtend(pxlfp, 4)) - 28;
  2821.             fseek(pxlfp, ptr->where.address.fileOffset + 36, 0);
  2822. /*
  2823. fprintf(stderr,"bytes=%d, seeking at %ld\n",
  2824.             bytes, ptr->where.address.fileOffset + 36);
  2825. */
  2826.         } else if ((ptr->flag_byte & 4) == 4) {
  2827.             bytes = ((long)ptr->flag_byte & 3)
  2828.                 * 65536l + NoSignExtend(pxlfp, 2) - 13;
  2829.             fseek(pxlfp, ptr->where.address.fileOffset + 16, 0);
  2830.         } else {
  2831.             bytes = ((long)ptr->flag_byte & 3)
  2832.                 * 256 + NoSignExtend(pxlfp, 1) - 8;
  2833.             fseek(pxlfp, ptr->where.address.fileOffset + 10, 0);
  2834.         }
  2835.     } else if (fontptr->id == id1002)
  2836.         bytes =  ((( (long)ptr->width + 7) >> 3) * (long) ptr->height);
  2837.     else if (fontptr->id == id1001)
  2838.         bytes =  4 * (((long)ptr->width + 31) >> 5) * (long)ptr->height;
  2839.  
  2840.     if (bytes > 0) {
  2841.                                           /* do NOT load Huge characters */
  2842.       if ((bytes > HUGE_CHAR_PATTERN) && (fontptr->id != pk89)) {
  2843.     qfprintf(stderr,"Huge Character <%c> (%ld Bytes)\n", (char)c, bytes);
  2844.         ptr->charsize = HUGE_SIZE;
  2845.         ptr->where.isloaded = FALSE;
  2846.       } else {
  2847.         if ( (pr = (long *)malloc( bytes )) == NULL )
  2848.             Fatal("Unable to allocate %ld bytes for char <%c>\n",
  2849.                          bytes, (char)c);
  2850. /*
  2851.  * else fprintf(stderr,"allocating %ld bytes char <%c>(%d)\t at 0x%lx\n",
  2852.  *                       bytes, (char)c,(int)c,pr);
  2853.  */ 
  2854. #ifdef DEBUG
  2855.         if (Debug)
  2856.           fprintf(stderr,
  2857.            "Allocating Char <%c>, FileOffset=%lX, Bytes=%ld (%d) <%d>\n",
  2858.               (char) c, ptr->where.address.fileOffset, bytes,
  2859.               (int)bytes, (unsigned int)bytes);
  2860. #endif
  2861.         allocated_storage += bytes;
  2862.         fread(pr, 1, (int) bytes , pxlfp);
  2863.         ptr->where.address.pixptr = pr;
  2864.       }
  2865.     } 
  2866. #else
  2867.     fseek(gfin, ptr->where.address.fileOffset, 0);
  2868.     gettochar();
  2869.     readbits();
  2870.     if (num_bytes > HUGE_CHAR_PATTERN)
  2871.       ptr->charsize = HUGE_SIZE;
  2872. #endif
  2873.     ptr->where.isloaded = TRUE;
  2874.     if ((ptr->charsize != SMALL_SIZE)
  2875. #ifdef LJ
  2876.          || (rasterfont[fontptr->plusid])
  2877. #endif
  2878.        )
  2879.         return;
  2880.  
  2881.     EmitChar(c, ptr);
  2882. #ifdef USEPXL
  2883.     /* we should really free the space used by the PXL data after this
  2884.        point, but it is not large, and besides, we may want to be
  2885.        more clever in the future, about sending bitmaps.  So keep
  2886.        the data around */
  2887. #endif
  2888. }
  2889. /*-->SetChar*/
  2890. /**********************************************************************/
  2891. /*****************************  SetChar  ******************************/
  2892. /**********************************************************************/
  2893. void
  2894. SetChar(c, command, PassNo, do_posn,in_string)
  2895. long    c;
  2896. short   command;
  2897. int     PassNo;
  2898. bool do_posn,in_string;
  2899. {
  2900.     register struct char_entry *ptr;  /* temporary char_entry pointer */
  2901.     bool pos_after = FALSE;
  2902.  
  2903.     ptr = &(fontptr->ch[c]);
  2904.     if (!((ptr->where.isloaded) || (ptr->charsize == HUGE_SIZE)))
  2905.         LoadAChar(c, ptr);
  2906.     if (PassNo == 0)
  2907.         return;
  2908.  
  2909.     if (do_posn) {
  2910. #ifdef IBM3812
  2911.         if (CharStringPos>0) {
  2912.             fprintf(stderr,"!!!! That should never happen!!!\n");
  2913.             CharStringOut;
  2914.         }
  2915. #endif
  2916.         SetPosn(h, v);
  2917.     }
  2918. /*
  2919. printf("(%d) hh=%ld (+%ld/+%ld), h=%ld, xh=%ld,xhh=%ld, [%ld|%ld] ->%d\n",
  2920.     (int)do_posn,hh,(long)ptr->cw,(long)ptr->cw*(long)hconv,h,
  2921.     PIXROUND(h, hconv),
  2922.     PIXROUND(hh, hconv),
  2923.     labs((hh-h)),hconv,(labs((hh-h))>hconv)
  2924.     );
  2925. */
  2926.     if (in_string && (labs((hh-h))>hconv)) {
  2927. #ifdef IBM3812
  2928.         CharStringOut;
  2929. #endif
  2930.         SetPosn(h, v);
  2931.     }
  2932.  
  2933.     if (fontptr->font_file_id != NO_FILE) {      /* ignore missing fonts */
  2934.         if ((ptr->charsize != SMALL_SIZE)
  2935. #ifdef LJ
  2936.          || (rasterfont[fontptr->plusid])
  2937. #endif
  2938.            ) {
  2939.             int     tmp;
  2940.             char    sign;
  2941.  
  2942. #ifdef LJ
  2943.             if (!do_posn)
  2944.                 SetPosn(h, v);
  2945. #ifdef USEPXL
  2946.             tmp = (int) -ptr->yOffset;
  2947. #else
  2948.             tmp = (int) num_rows-y_offset;
  2949. #endif
  2950.             if (tmp != 0) {
  2951.                if (tmp < 0) {
  2952.                    sign = '-'; tmp = -tmp;
  2953.                } else
  2954.                    sign = '+';
  2955.                EMIT(outfp, "\033*p%c%dY", sign, tmp);
  2956.             }
  2957. #ifdef USEPXL
  2958.             tmp = (int) -ptr->xOffset;
  2959. #else
  2960.             tmp = (int) -x_offset;
  2961. #endif
  2962.             if (tmp != 0) {
  2963.                if (tmp < 0) {
  2964.                    sign = '-'; tmp = -tmp;
  2965.                } else
  2966.                    sign = '+';
  2967.                EMIT(outfp, "\033*p%c%dX", sign, tmp);
  2968.             }
  2969. #endif
  2970. #ifdef IBM3812
  2971.             CharStringOut;
  2972. #endif
  2973. #ifdef DEBUG
  2974.     if (Debug)
  2975.         fprintf(stderr,"Raster character <%c> %hd\n", (char) c,(short)c);
  2976. #endif
  2977.             RasterChar(ptr);
  2978.             pos_after = TRUE;
  2979.         } else {
  2980. #ifdef IBM3812
  2981. #ifdef USEPXL
  2982.             if ( ptr->yyOffset || (!in_string) ) {
  2983.                 CharStringOut;
  2984.                 MoveVert(ptr->yyOffset);
  2985.                 sprintf(PMPformat, "\01%c", (unsigned char)c);
  2986.                 PMPout(2, PMPformat);
  2987.                 MoveVert((int)-(ptr->yyOffset));
  2988.             } else {
  2989. #endif
  2990.                 if (CharStringPos==CHARSTRINGMAX)
  2991.                     CharStringOut;
  2992.  
  2993.                 CharString[CharStringPos]=(unsigned char)c;
  2994.                 CharStringPos++;
  2995. #ifdef USEPXL
  2996.             }
  2997. #endif
  2998. #endif
  2999. #ifdef LJ
  3000. #define TRANSPARENTCHAR(c) \
  3001.         if ((c == 0l) || (c >= 7l && c <= 15l) || (c == 27l)) \
  3002.         EMIT(outfp, "\033&p1X%c", (unsigned char)c); \
  3003.         else EMITC((unsigned char)c)
  3004. #ifdef USEPXL
  3005.             if (ptr->yyOffset) {
  3006.                 EMIT(outfp, "\033*p+%hdY", ptr->yyOffset);
  3007.                 TRANSPARENTCHAR(c);
  3008.                 EMIT(outfp, "\033*p-%hdY", ptr->yyOffset);
  3009.             } else
  3010. #endif
  3011. /*                EMITC( (unsigned char)c);*/
  3012.                { TRANSPARENTCHAR(c);}
  3013. #endif
  3014.         }
  3015.         hh += (long) ptr->cw*hconv;
  3016.     }
  3017.     if (command <= SET4)
  3018.         h += ptr->tfmw;
  3019.     if (pos_after)
  3020.         SetPosn(h, v);
  3021. }
  3022.  
  3023.  
  3024. void
  3025. DoBop()
  3026. {
  3027.     struct font_entry *p;
  3028. #ifdef LJ
  3029.     register short i;
  3030.     if (fonts_used_on_this_page > MAX_FONTS_PER_PAGE) {
  3031.        for (i = 0; i < HANDLE_MAX_FONTS; i++)
  3032.           rasterfont[i] = FALSE;
  3033.     }
  3034.     fonts_used_on_this_page = 0;
  3035. #endif
  3036.     for (p = hfontptr; p; p = p->next) {
  3037.         p->used_on_this_page = FALSE;
  3038.     }
  3039. }
  3040.  
  3041.  
  3042. /*-->SetFntNum*/
  3043. /**********************************************************************/
  3044. /****************************  SetFntNum  *****************************/
  3045. /**********************************************************************/
  3046. void
  3047. SetFntNum(k, Emitting)
  3048. long    k;
  3049. bool Emitting;
  3050. /*  this routine is used to specify the font to be used in printing future
  3051.     characters */
  3052. {
  3053. #ifdef LJ
  3054.     static unsigned short plusid = 0;
  3055. #endif
  3056.     fontptr = hfontptr;
  3057.     while ((fontptr != NULL) && (fontptr->k != k))
  3058.         fontptr = fontptr->next;
  3059.     if (fontptr == NULL)
  3060.         Fatal("font %ld undefined", k);
  3061.     if (Emitting && (fontptr->font_file_id != NO_FILE) ) {
  3062.         if (!fontptr->used_on_this_page) {
  3063.             fontptr->used_on_this_page = TRUE;
  3064. #ifdef LJ
  3065.             if (++fonts_used_on_this_page > MAX_FONTS_PER_PAGE) {
  3066.               qfprintf(stderr,"this is the %d. font on this page!",
  3067.                        fonts_used_on_this_page);
  3068.               qfprintf(stderr," (max = %d) rastering characters!\n",
  3069.                        MAX_FONTS_PER_PAGE);
  3070.                rasterfont[fontptr->plusid] = TRUE;
  3071.             }
  3072. #endif
  3073.         }
  3074.  
  3075.         /* activate font */
  3076. #ifdef IBM3812
  3077.         sprintf(PMPformat, "\323%c", (unsigned char)fontptr->plusid);
  3078.         PMPout(2, PMPformat);
  3079. #endif
  3080. #ifdef LJ
  3081.         if (!rasterfont[fontptr->plusid]) {
  3082.             if (fontptr->plusid>0) EMIT(outfp, "\033(%dX", fontptr->plusid);
  3083.             else                   EMIT(outfp, "\033(X");
  3084.         }
  3085. /* else printf("I am doing rasterfont for plusid=%d instead\n",
  3086.                 fontptr->plusid);
  3087. */
  3088. #endif
  3089.     }
  3090. #ifdef LJ    /* reassignment of printer font id  0.48 */
  3091.     else if (fontptr->font_file_id != NO_FILE) {
  3092.             if (fontptr->ncdl == 0) {
  3093. #ifdef DEBUG
  3094.         printf("Changing plusid from %d to %d\n", fontptr->plusid, (int)plusid);
  3095. #endif
  3096.                 fontptr -> plusid = plusid;
  3097.                 plusid ++;
  3098.             }
  3099.     }
  3100. #endif
  3101. }
  3102.  
  3103.  
  3104. /*-->SetPosn*/
  3105. /**********************************************************************/
  3106. /*****************************  SetPosn  ******************************/
  3107. /**********************************************************************/
  3108. void                  /* output a positioning command */
  3109. SetPosn(x, y)
  3110. long    x, y;
  3111. {
  3112.     int     rx, ry;
  3113.     rx = (int)PIXROUND(x, hconv) + x_goffset;
  3114.     ry = (int)PIXROUND(y, vconv) + y_goffset;
  3115.  
  3116. /*
  3117.  * printf("setposn to %d/%d\n",rx,ry);
  3118.  */
  3119.  
  3120. #ifdef IBM3812
  3121.     PMPcont(3);
  3122.     PMPoutC('\340');
  3123.     EMITWORD(LARGER(rx,0));
  3124.  
  3125.     if (last_ry != ry) { /* necessary to set new y-position */
  3126.         PMPcont(3);
  3127.         PMPoutC('\341');
  3128.         EMITWORD(LARGER(ry,0));
  3129.     }
  3130. #endif
  3131. #ifdef LJ
  3132.     if (last_ry != ry)   /* necessary to set new y-position */
  3133.         EMIT(outfp, "\033*p%dx%dY", LARGER(rx,0), LARGER(ry,0));
  3134.     else
  3135.         EMIT(outfp, "\033*p%dX", LARGER(rx,0));
  3136. #endif
  3137.  
  3138.     last_ry = ry;    /* last y-position on output device */
  3139.     last_rx = rx;    /* last x-position on output device */
  3140. /*
  3141.  * must know where device "really" is horizontally, for rel. posning.
  3142.  * (maybe in the future), but we always use direct positioning for
  3143.  * vertical movement.
  3144.  */
  3145.     /* hh = rx * hconv; */
  3146.     hh = x;
  3147.     vv = y;
  3148. /*
  3149.  *     fprintf(stderr,"DoPosn: x=%ld, y=%ld, rx=%d, ry=%d, hh=%ld, vv=%ld\n",
  3150.  *               x,y,rx,ry,hh,vv);
  3151.  */
  3152. }
  3153.  
  3154.  
  3155. #ifdef IBM3812
  3156. /*-->PMPLine*/
  3157. /**********************************************************************/
  3158. /*****************************  PMPLine  ******************************/
  3159. /**********************************************************************/
  3160. void       /* drawing lines on the 3812 using PMP vector commands */
  3161. PMPLine(w, y, x)
  3162. int     w, y, x;
  3163. {
  3164.  
  3165.     if ((w == 0) || ((x == 0) && (y == 0)))
  3166.         return;
  3167.  
  3168. /*
  3169. fprintf(stderr,"w=%d / %d - %d, y=%d / %d - %d, x=%d / %d - %d\n",
  3170.         w,(char)(w & 0xff),(int)((signed_char)(w & 0xff)),
  3171.         y,(char)(y & 0xff),(int)((signed_char)(y & 0xff)),
  3172.         x,(char)(x & 0xff),(int)((signed_char)(x & 0xff)));
  3173. */
  3174.  
  3175.     if ( (((signed_char)(x & 0xff)) == x ) &&
  3176.         ( ((signed_char)(y & 0xff)) == y ) ) {
  3177.         PMPcont(6);
  3178.         PMPout(1, "\370");
  3179.         EMITWORD(3);      /* length of vector */
  3180.         PMPoutC((unsigned char)(0x80 | 0x00 | (unsigned char) w));
  3181.         PMPoutC((signed_char)(y & 0xff));
  3182.         PMPoutC((signed_char)(x & 0xff));
  3183. /*
  3184.         fprintf(stderr,"F8 00 03: w=%d, x=%d(%d-%.2X), y=%d(%d-%.2X),\n",
  3185.         w,x,(char)(x & 0xff),(signed_char)(x & 0xff),
  3186.           y,(char)(y & 0xff),(signed_char)(y & 0xff));
  3187. */
  3188.  
  3189.     } else {
  3190.         PMPcont(8);
  3191.         PMPout(1, "\370");
  3192.         EMITWORD(4 + 1);      /* length of vector */
  3193.         PMPoutC((unsigned char)(0xC0 | 0x00 | (unsigned char) w));
  3194.         EMITWORD(y);
  3195.         EMITWORD(x);
  3196. /*
  3197.         fprintf(stderr,"F8 00 05: w=%d, x=%d, y=%d,\n", w,x,y);
  3198. */
  3199.     }
  3200. }
  3201.  
  3202.  
  3203. #endif
  3204. /*-->SetRule*/
  3205. /**********************************************************************/
  3206. /*****************************  SetRule  ******************************/
  3207. /**********************************************************************/
  3208. void                   /*   this routine will draw a rule */
  3209. SetRule(a, b, Set)
  3210. long    a, b;
  3211. int     Set;
  3212. {
  3213.     long    xx, yy;
  3214.     short   hor_offset, vert_offset, ll;
  3215.  
  3216.     if ( a > 0 && b > 0 ) {
  3217.         SetPosn(h, v);             /* lower left corner */
  3218.         xx = (long)PIXROUND(b, hconv);     /* width */
  3219.         yy = (long)PIXROUND(a, vconv);     /* height */
  3220.  
  3221. #ifdef DEBUG
  3222.         if (Debug)
  3223.             fprintf(stderr,"Rule xx=%ld, yy=%ld\n", xx, yy);
  3224. #endif
  3225.  
  3226.         hor_offset  = (short)(last_ry - yy);
  3227.         if (hor_offset < 0) yy += hor_offset;
  3228.         if (last_rx < 0) xx += last_rx;
  3229.  
  3230. #ifdef IBM3812
  3231.         if (Landscape) {
  3232.           if (last_ry > MAX_PAGE_WIDTH) yy += MAX_PAGE_WIDTH-last_ry;
  3233.           hor_offset  = (short)(MAX_PAGE_HEIGHT - (last_rx + xx));
  3234.         } else {
  3235.           if (last_ry > MAX_PAGE_HEIGHT) yy += MAX_PAGE_HEIGHT-last_ry;
  3236.           hor_offset  = (short)(MAX_PAGE_WIDTH - (last_rx + xx));
  3237.         }
  3238.         if (hor_offset < 0) xx += hor_offset;
  3239.  
  3240.         if ((xx > 31) && (yy > 31)) {
  3241. /*
  3242.  *   fill area by multiple lines  (kind of a mess)
  3243.  *   process for simplicity always horizontally
  3244.  */
  3245.  
  3246. /* printf("large box: w=%d, x=%d, y=%d\n",(int)yy,(int)xx,0);*/
  3247.  
  3248.             hor_offset  = HOR_HALF(30);
  3249.             MoveHor(hor_offset);
  3250.             vert_offset = VERT_HALF(30);
  3251.             MoveVert(-vert_offset);
  3252.             ll = (short)xx - 30;
  3253.  
  3254.             for (; yy > 30; yy -= 30) {
  3255.                 PMPLine(30, 0, ll);
  3256.                 MoveHor(-ll);
  3257.                 MoveVert(-30);
  3258.             }
  3259.  
  3260.             hor_offset  = -hor_offset     + HOR_HALF(yy);
  3261.             MoveHor(hor_offset);
  3262.             vert_offset = (vert_offset - 30) + VERT_HALF(yy);
  3263.             MoveVert(-vert_offset);
  3264.  
  3265.             PMPLine((int)yy, 0, (int)(xx - yy));
  3266.  
  3267.         } else if ( (yy < xx) && (xx > 0) ) {
  3268.  
  3269. /* printf("hori rule: w=%d, x=%d, y=%d\n",(int)yy,(int)(xx-yy),0);*/
  3270.  
  3271.             hor_offset  = HOR_HALF(yy);
  3272.             vert_offset = VERT_HALF(yy);
  3273.  
  3274.             MoveHor(hor_offset);
  3275.             MoveVert(-vert_offset);
  3276.  
  3277.             PMPLine((int)yy, 0, (int)(xx - yy));
  3278.         } else if ( (xx < yy) && (yy > 0)) {
  3279.  
  3280.             hor_offset  = HOR_HALF(xx);
  3281.             vert_offset = VERT_HALF(xx);
  3282. /*
  3283.  printf("move: x=%d, y=%d\n",hor_offset,-vert_offset);
  3284.  printf("vert rule: w=%d, x=%d, y=%d\n",(int)xx,0,(int)-(yy-xx));
  3285. */
  3286.             MoveHor(hor_offset);
  3287.             MoveVert(-vert_offset);
  3288.  
  3289.             PMPLine((int)xx, (int)-(yy - xx), 0);
  3290.         } else if (xx == yy) {
  3291.             short     y0;  /* small square box!! */
  3292.  
  3293.             y0 = (short)yy / 2;
  3294.             hor_offset  = HOR_HALF(y0);
  3295.             MoveHor(hor_offset);
  3296.             vert_offset = VERT_HALF(y0);
  3297.             MoveVert(-vert_offset);
  3298.             ll = (short)xx - y0;
  3299.  
  3300.             PMPLine((int)y0, 0, ll);
  3301.  
  3302.             hor_offset  = -(ll + hor_offset);
  3303.             vert_offset = (y0 - vert_offset);
  3304.  
  3305.             yy -= (long)y0;
  3306.             hor_offset  += HOR_HALF(yy);
  3307.             MoveHor(hor_offset);
  3308.             vert_offset += VERT_HALF(yy);
  3309.             MoveVert(-vert_offset);
  3310.  
  3311.             PMPLine((int)yy, 0, (int)xx - yy);
  3312.         }
  3313. #endif
  3314. #ifdef LJ
  3315.         if ((pgsiz_dots >0) && (last_ry > pgsiz_dots))
  3316.                 yy += (long)pgsiz_dots - (long)last_ry;
  3317.  
  3318.         if ((yy>0) && (xx>0))
  3319.                 EMIT(outfp, "\033*p-%ldY\033*c%lda%ldbP", yy - 1, xx, yy);
  3320. #endif
  3321.         last_ry = UNKNOWN;       /* next time full positioning */
  3322.     }
  3323.     if (Set)
  3324.         h += b;
  3325. }
  3326.  
  3327.  
  3328. /*-->SetString*/
  3329. /**********************************************************************/
  3330. /*****************************  SetString  ****************************/
  3331. /**********************************************************************/
  3332. void
  3333. SetString(firstch, PassNo)    /* read and set a consecutive string of chars */
  3334. short   firstch;
  3335. int     PassNo;
  3336. {
  3337.     short   c;
  3338.     register unsigned short i;
  3339.  
  3340. #ifdef DEBUG
  3341.     if (Debug)
  3342.       fprintf(stderr, "SetString ");
  3343. #endif
  3344.     for (i = 0, c = firstch; c >= SETC_000 && c <= SETC_127; i++) {
  3345. #ifdef DEBUG
  3346.         if (Debug)
  3347.           fprintf (stderr, "%d ", c);
  3348. #endif
  3349.         SetChar((long)c,  c, PassNo, (bool)(i==0),TRUE);
  3350.         c = (short) NoSignExtend(dvifp, 1);
  3351.     }
  3352.     fseek(dvifp, -1l, 1);    /* backup one character */
  3353. #ifdef IBM3812
  3354.     CharStringOut;
  3355. #endif
  3356. #ifdef DEBUG
  3357.     if (Debug)
  3358.       fprintf(stderr, "...SetString\n");
  3359. #endif
  3360. }
  3361.  
  3362.  
  3363. /*-->SignExtend*/
  3364. /**********************************************************************/
  3365. /****************************  SignExtend  ****************************/
  3366. /**********************************************************************/
  3367. long
  3368. SignExtend(fp, n)   /* return n byte quantity from file fd */
  3369. register FILE *fp;  /* file pointer    */
  3370. register int    n;  /* number of bytes */
  3371. {
  3372.     int     n1;     /* number of bytes      */
  3373.     long    x;      /* number being constructed */
  3374.     x = getc(fp);   /* get first (high-order) byte */
  3375.     n1 = n--;
  3376.     while (n--)  {
  3377.         x <<= 8;
  3378.         x |= getc(fp);
  3379.     }
  3380. /*
  3381.  *   NOTE: This code assumes that the right-shift is an arithmetic, rather
  3382.  *   than logical, shift which will propagate the sign bit right.   According
  3383.  *   to Kernighan and Ritchie, this is compiler dependent!
  3384.  */
  3385.     x <<= 32 - 8 * n1;
  3386.     x >>= 32 - 8 * n1; /* sign extend */
  3387. #ifdef DEBUG
  3388.     if (Debug)
  3389.         fprintf(stderr,"\tSignExtend(fp,%d)=%X\n", n1, x);
  3390. #endif
  3391.     return(x);
  3392. }
  3393.  
  3394.  
  3395. /*-->SkipFontDef*/
  3396. /**********************************************************************/
  3397. /****************************  SkipFontDef  ***************************/
  3398. /**********************************************************************/
  3399. void
  3400. SkipFontDef()
  3401. {
  3402.     int     a, l;
  3403.     char    n[STRSIZE];
  3404.  
  3405.     (void) NoSignExtend(dvifp, 4);
  3406.     (void) NoSignExtend(dvifp, 4);
  3407.     (void) NoSignExtend(dvifp, 4);
  3408.     a = (int) NoSignExtend(dvifp, 1);
  3409.     l = (int) NoSignExtend(dvifp, 1);
  3410.     GetBytes(dvifp, n, a + l);
  3411. }
  3412.  
  3413.  
  3414. /*-->Warning*/
  3415. /**********************************************************************/
  3416. /*****************************  Warning  ******************************/
  3417. /**********************************************************************/
  3418. void                           /* issue a warning */
  3419. Warning(fmt, a, b, c, d)
  3420. char    *fmt;         /* format    */
  3421. char    *a, *b, *c, *d;   /* arguments */
  3422. {
  3423.     G_errenc = 1;
  3424.     if ( G_nowarn || G_quiet )
  3425.         return;
  3426.  
  3427.     fprintf(stderr, "warning : ");
  3428.     fprintf(stderr, fmt, a, b, c, d);
  3429.     fprintf(stderr, "\n");
  3430. }
  3431.  
  3432. void
  3433. PutWord(w)
  3434. int     w;
  3435. {
  3436.     EMITC((char)(w >> 8) & 0xff);
  3437.     EMITC((char)w & 0xff);
  3438. }
  3439.  
  3440.  
  3441. #ifdef IBM3812
  3442. /*-->PMPout*/
  3443. /*****************************************************************************/
  3444. /* This routine produces the PMP-envelopes for the 3812. Its semantics are:
  3445.  
  3446.    first arg == 0  ... flush buffer
  3447.    first arg == -1 ... number of bytes specified in the second argument
  3448.                have to be continuous, that is they must not
  3449.                be disrupted by ENTER PMP etc.
  3450.    first arg > 0       output first arg bytes
  3451.  
  3452.                If arg2 > OUTBUFSIZE ... flush buffer,
  3453.                         switch to unbuffered mode
  3454.                         (dont't collect PMP commands)
  3455.                If arg2+bufferpointer > OUTBUFSIZE ... flush buffer,
  3456.                         block will fit into buffer
  3457.                otherwise ..... block will fit into buffer
  3458.  
  3459.   Buffering is done to reduce the ENTER PMP-commands. Initially
  3460.   the 3812 is in PC-ASCII mode. In order to issue a PMP-command it is
  3461.   necessary to enter PMP mode. The ENTER-PMP-command contains the
  3462.   number of bytes that will be interpreted as PMP-commands. In the
  3463.   most naive approach for each primitive command (eg. set cursor) you
  3464.   have to produce a seperate ENTER-PMP-envelope (5 bytes). It is
  3465.   favourable to collect as many PMP commands as possible in one envelope. */
  3466. /*****************************************************************************/
  3467. void
  3468. PMPout(l, s)
  3469. char    *s;
  3470. int     l;
  3471. {
  3472.     static char     buffer[OUTBUFSIZE];
  3473.     static unsigned short   bp = 0;         /* range 0..OUTBUFSIZE */
  3474.     static long     continuous = 0l;
  3475.     static bool buffered = TRUE;
  3476.  
  3477.     if (l == 0) {
  3478.         if (bp == 0)
  3479.             return;
  3480.         EMIT(outfp, "\033[C%c%c", (unsigned char)(bp & 0xFF),
  3481.             (unsigned char)(bp >> 8));
  3482.         EMITB((int)bp, buffer);
  3483.         bp = 0;
  3484.         return;
  3485.     }
  3486.     if (l == -1) {
  3487.         continuous = (long)s;
  3488.         if (continuous + (long)bp + 5l > (long) OUTBUFSIZE)
  3489.             PMPflush;
  3490.         buffered = (bool) ((continuous + 5l <= (long) OUTBUFSIZE));
  3491.         if (!buffered) {
  3492.             EMIT(outfp, "\033[C%c%c",
  3493.                 (unsigned char)(continuous & 0xFF),
  3494.                 (unsigned char)((continuous >> 8) & 0xFF));
  3495.         }
  3496.         return;
  3497.     }
  3498.     if (buffered) {
  3499.         register int    i;
  3500.         if ( ((long)l + bp) > OUTBUFSIZE)
  3501.             PMPflush;
  3502.         for (i = 0; i < l; i++)
  3503.             buffer[bp+i] = s[i];
  3504.         bp += (unsigned short)l;
  3505.     } else {
  3506.         EMITB((int)l, s);
  3507.         buffered = (bool) ((continuous -= (long)l) <= 0) ;
  3508.     }
  3509. }
  3510.  
  3511.  
  3512. void
  3513. PMPoutC(c)
  3514. char    (c);
  3515. {
  3516.     PMPout(1, &c);
  3517. }
  3518.  
  3519.  
  3520. #endif
  3521. #ifdef MSDOS
  3522. /*-->AssureBinary*/
  3523. /**********************************************************************/
  3524. /*************************** AssureBinary *****************************/
  3525. /**********************************************************************/
  3526. /* This procedure is both DOS AND MSC dependent. The MSC file open on */
  3527. /* a device ignores the 'binary' of the "wb" parameter and opens the  */
  3528. /* file in ascii mode. This procedure sets the file f to binary mode  */
  3529. /* if it is connected to a device that is not console input or output */
  3530. /* or the null device. For other operating systems this routine is    */
  3531. /* useless. (Background: MSDOS 3.2 Technical Reference upd 1 pg 6-137 */
  3532. /**********************************************************************/
  3533. void
  3534. AssureBinary(f)
  3535. FILE *f;
  3536. {
  3537.     union REGS regs;              /* registers for bios call */
  3538.  
  3539.     regs.h.ah = (unsigned char) 0x44;     /* IOCTL            */
  3540.     regs.h.al = (unsigned char) 0x00;     /* get device information   */
  3541.     regs.x.bx = (unsigned int) fileno(f); /* handle from MSC      */
  3542.     intdos(®s, ®s);         /* call DOS interrupt       */
  3543.                           /* ---> result in DX    */
  3544.  
  3545.     if (  (regs.h.dl & 0x80)     /* file handle points to a device */
  3546.          && !(regs.h.dl & 0x07) )    /* neither console i/o or null    */ {
  3547.  
  3548.         regs.h.dl  |= 0x20;      /* set BINARY bit in device info  */
  3549.  
  3550.         regs.h.ah = (unsigned char) 0x44;    /* IOCTL         */
  3551.         regs.h.al = (unsigned char) 0x01;    /* set device information*/
  3552.         regs.x.bx = (unsigned int) fileno(f); /* handle from MSC      */
  3553.         regs.h.dh = (unsigned char) 0x00;    /* clear DH          */
  3554.         intdos(®s, ®s);           /* call DOS interrupt     */
  3555.     }
  3556. }
  3557.  
  3558.  
  3559. #endif
  3560.  
  3561. #ifdef USEPXL
  3562. bool getbit ();
  3563. unsigned char   getnyb ();
  3564. long    pk_packed_num ();
  3565.  
  3566.  
  3567. #define  PKBYTE   *pkloc; pkloc ++
  3568. #define  OUTCHAR(c) raster_line_buf[bp]= (unsigned char)c; bp++
  3569.  
  3570. unsigned char   bitweight, inputbyte ;
  3571. unsigned char   dyn_f ;
  3572. unsigned char   *pkloc;
  3573. int     repeatcount;
  3574.  
  3575. void              /* <Read and translate raster description@>*/
  3576. PkRaster(ce, raster)
  3577. struct char_entry *ce;
  3578. bool raster;
  3579. {
  3580.     int     rp;
  3581.     int     current_line;
  3582.     int     wordwidth ;
  3583.     bool turnon;
  3584.     unsigned short  nbpl;
  3585.     long    rowsleft, word, wordweight, hbit, count, i, j, tl;
  3586.     long    row[101] ;
  3587.     unsigned char   raster_line_buf[BYTES_PER_PIXEL_LINE];
  3588.     unsigned short  bp;
  3589.  
  3590.  
  3591.     if (ce->charsize == HUGE_SIZE)
  3592.         Fatal( "cannot process currently PK font patterns of that size!\n");
  3593.  
  3594.  
  3595.     current_line = 0;
  3596.     pkloc = (unsigned char *)ce->where.address.pixptr;
  3597.     dyn_f = (unsigned char)(ce->flag_byte >> 4);
  3598.     turnon = (bool)((ce->flag_byte & 8) == 8);
  3599.     wordwidth = (int)(ce->width + 31) >> 5 ;
  3600.     nbpl = ((ce->width) +  7) >> 3;
  3601.  
  3602.     bitweight = 0 ;
  3603.     if (dyn_f == 14) {
  3604.         /*printf("<Get raster by bits@>\n");*/
  3605.         for (i = 1; i <= (long)ce->height; i++) {
  3606.         word = 0 ;
  3607.         wordweight = 31 ;
  3608.         bp = 0;            /* Sowa */
  3609.  
  3610. #ifdef DRAWGLYPH
  3611.            printf("     |");
  3612. #endif
  3613.         for (j = 1; j <= (long) ce->width; j++) {
  3614.             bool getbit;
  3615.             /* bp = 0;               SOWA/
  3616. /*******************************************begin Getbit *********/
  3617.             bitweight /= 2 ;
  3618.             if ( bitweight == 0 ) {
  3619.                 inputbyte = PKBYTE ;
  3620.                 bitweight = 128 ;
  3621.             }
  3622.             getbit = (bool)
  3623.              ( inputbyte >= bitweight ) ;
  3624.             if ( getbit )
  3625.                 inputbyte -= bitweight ;
  3626. /*********************************************end Getbit *********/
  3627.  
  3628.             if (getbit)
  3629.                 word += power[wordweight] ;
  3630.  
  3631.             wordweight --;
  3632.             if (wordweight == -1) {
  3633.  
  3634. #ifdef DRAWGLYPH
  3635.    { int k;
  3636.      for (k=31; k>=0; k--) {
  3637.          if ((power[k] & word)!=0) printf("M");
  3638.          else printf(".");
  3639.      }
  3640.    }
  3641. #endif
  3642.  
  3643.             OUTCHAR((word >> 24 & 0xFF));
  3644.             OUTCHAR((word >> 16 & 0xFF));
  3645.             OUTCHAR((word >> 8 & 0xFF));
  3646.             OUTCHAR((word    & 0xFF));
  3647.  
  3648.             word = 0 ;
  3649.             wordweight = 31 ;
  3650.             }
  3651.         }
  3652.         if (wordweight < 31) {
  3653. #ifdef COMMENT
  3654. #ifdef DRAWGLYPH
  3655.    { int k;
  3656.      for (k=15; k>=0; k--) {
  3657.         if ((power[k] & word)!=0) printf("Z");
  3658.         else printf(":");
  3659.      }
  3660.     }
  3661.     printf("|\n ----|");
  3662. #endif
  3663. #endif
  3664.  
  3665.             for (j = 3; j >= (wordwidth * 4 - (long)nbpl);
  3666.             j--) {
  3667.  
  3668.                 OUTCHAR(((word >> (j << 3)) & 0xff));
  3669.  
  3670. #ifdef DRAWGLYPH
  3671.    { int k;
  3672.      for (k=7; k>=0; k--) {
  3673.         if ((power[k] & ((word >> (j << 3)) & 0xff))!=0) printf("M");
  3674.         else printf(".");
  3675.      }
  3676.    }
  3677. #endif
  3678.  
  3679.             }
  3680.         }
  3681.  
  3682.         if (raster) {
  3683.             RasterLine(ce, (unsigned int)nbpl,
  3684.                 current_line, raster_line_buf);
  3685.             current_line++;
  3686.         } else
  3687.             EMITL(bp, raster_line_buf);
  3688.  
  3689. #ifdef DRAWGLYPH
  3690.    printf("|\n");
  3691. #endif
  3692.         }
  3693.     } else {
  3694.         /* printf("@<Create normally packed raster@>\n"); */
  3695.         rowsleft = (long) ce->height ;
  3696.         hbit = (long) ce->width ;
  3697.         repeatcount = 0 ;
  3698.         wordweight = 32 ;
  3699.         word = 0 ;
  3700.         rp = 1 ;
  3701.         while ( rowsleft > 0 ) {
  3702.         count = pk_packed_num() ;
  3703.         bp = 0;
  3704.  
  3705.         while (count > 0) {
  3706.             if ((count < wordweight) && (count < hbit)) {
  3707.             if (turnon)
  3708.                 word +=
  3709.                     gpower[wordweight] -
  3710.                     gpower[wordweight - count] ;
  3711.  
  3712.             hbit -= count ;
  3713.             wordweight -= count ;
  3714.             count = 0 ;
  3715.             } else if ((count >= hbit) && (hbit <=
  3716.             wordweight)) {
  3717.  
  3718.             if (turnon)
  3719.                 word +=
  3720.                     gpower[wordweight] -
  3721.                     gpower[wordweight - hbit] ;
  3722.  
  3723.             row[rp] = word ;
  3724.  
  3725.             /* printf(" @<Send row@> \n");*/
  3726.             for (i = 0; i <= (long) repeatcount; i++) { int ii;
  3727.  
  3728. #ifdef DRAWGLYPH
  3729.   printf("***  |");
  3730. #endif
  3731.                 for (ii = 1; ii < wordwidth; ii++) {
  3732.                 tl = row[ii];
  3733.  
  3734.                 OUTCHAR((tl >> 24 & 0xFF));
  3735.                 OUTCHAR((tl >> 16 & 0xFF));
  3736.                 OUTCHAR((tl >> 8  & 0xFF));
  3737.                 OUTCHAR((tl       & 0xFF));
  3738.  
  3739. #ifdef DRAWGLYPH
  3740.    { int k;
  3741.      for (k=31; k>=0; k--)  {
  3742.          if ((power[k] & row[ii])!=0) printf("M");
  3743.          else printf(".");
  3744.      }
  3745.    }
  3746. #endif
  3747.                 }
  3748.                 tl = row[wordwidth];
  3749.                 for (j = 3; j >= (wordwidth *4 - (long)nbpl);
  3750.                  j--) {
  3751.  
  3752.                  OUTCHAR(((tl >> (j << 3)) & 0xff));
  3753.  
  3754. #ifdef DRAWGLYPH
  3755.    { int k;
  3756.      for (k=7; k>=0; k--) {
  3757.          if ((power[k] & ((tl >> (j << 3)) & 0xff))!=0) printf("M");
  3758.          else printf(".");
  3759.      }
  3760.    }
  3761. #endif
  3762.                 }
  3763.  
  3764.                 if (raster) {
  3765.                     RasterLine(ce,
  3766.                     (unsigned int)nbpl,
  3767.                      current_line,
  3768.                      raster_line_buf);
  3769.                     current_line++;
  3770.                 } else
  3771.                     EMITL(bp, raster_line_buf);
  3772.  
  3773.                 bp = 0;
  3774.  
  3775. #ifdef DRAWGLYPH
  3776.    printf("|  ");
  3777.    for (j=1;j<=(long)wordwidth;j++) printf("%02lX/",row[j]);
  3778.    printf(" raster=%d\n",raster);
  3779. #endif
  3780.             }
  3781.  
  3782.             rowsleft -=  (long)repeatcount + 1 ;
  3783.             repeatcount = 0 ;
  3784.             rp = 1 ;
  3785.             word = 0 ;
  3786.             wordweight = 32 ;
  3787.             count -= hbit ;
  3788.             hbit = (long)ce->width ;
  3789.             } else {
  3790.             if (turnon) word += gpower[wordweight] ;
  3791.             row[rp] = word ;
  3792.             rp = rp + 1 ;
  3793.             word = 0 ;
  3794.             count -= wordweight ;
  3795.             hbit -= wordweight ;
  3796.             wordweight = 32 ;
  3797.             }
  3798.         }   /* .....while count > 0 */
  3799.         if (turnon)
  3800.             turnon = FALSE;
  3801.         else
  3802.             turnon = TRUE;
  3803.         } /* ...... rowsleft > 0 */
  3804.         if ((rowsleft != 0) || (hbit != (long)ce->width))
  3805.             Fatal("Bad pk file----more bits than required!\n");
  3806.     } /* .... create normally packed raster */
  3807. }
  3808.  
  3809.  
  3810. unsigned char   getnyb ()
  3811. {
  3812.     register unsigned char  temp ;
  3813.     if ( bitweight == 0 ) {
  3814.         inputbyte = PKBYTE ;
  3815.         bitweight = 16 ;
  3816.     }
  3817.     temp = inputbyte / bitweight ;
  3818.     inputbyte -= temp * bitweight ;
  3819.     bitweight /= 16 ;
  3820.     return ( temp ) ;
  3821. }
  3822.  
  3823.  
  3824. long
  3825. pk_packed_num ()
  3826. { /*@<Packed number procedure@>= */
  3827.     register int    i;
  3828.     long    j;
  3829.  
  3830.     i = (int)getnyb();
  3831.     if (i == 0) {
  3832.         do {
  3833.             j = (long)getnyb();
  3834.             i++;
  3835.         } while (j == 0) ;
  3836.         while (i > 0) {
  3837.             j = j * 16 + (long)getnyb() ;
  3838.             i--;
  3839.         };
  3840.         return (j - 15 + (13 - dyn_f) * 16 + dyn_f) ;
  3841.     } else if (i <= (int)dyn_f) {
  3842.         return ((long)i);
  3843.     } else if (i < 14) {
  3844.         return ((i-(long)dyn_f - 1) * 16 + (long)getnyb() + dyn_f + 1);
  3845.     } else {
  3846.         if (i == 14) {
  3847.             repeatcount = (int) pk_packed_num() ;
  3848.         } else {
  3849.             repeatcount = 1 ;
  3850.         }
  3851.         /*      printf("repeatcount = [%d]\n",repeatcount);    */
  3852.         return (pk_packed_num()) ;    /* tail end recursion !! */
  3853.     }
  3854. }
  3855. #endif  
  3856.  
  3857. #ifndef USEPXL
  3858. void bad_gf(n)
  3859.     int n;
  3860. {
  3861.     Fatal("Bad gf file, case %d\n",n);      /* See gf.c */
  3862. }
  3863. #endif
  3864. /*-->FormFeed*/
  3865. /**********************************************************************/
  3866. /*****************************  FormFeed ******************************/
  3867. /**********************************************************************/
  3868. void
  3869. FormFeed()
  3870. {
  3871.  
  3872. #ifdef IBM3812
  3873.     unsigned short pages;
  3874.     if ( (ndone == 0) && (FirstAlternate)){
  3875.         for (pages = 1; pages < ncopies; pages++) {
  3876.             PMPout(2, "\321\300"); /* PMP-command xD1C0 */
  3877.         }
  3878.         PMPout(2, "\321\100"); /* PMP-command xD140 */
  3879.     } else {
  3880.         for (pages = 1; pages < ncopies; pages++){
  3881.             PMPout(2, "\321\200"); /* PMP-command xD180 */
  3882.         }
  3883.         PMPout(2, "\321\0"); /* PMP-command xD100 */
  3884.     }
  3885. #endif
  3886. #ifdef LJ
  3887.     EMITC('\f');
  3888. #endif
  3889. }
  3890.